I have a model called "Activity" in my django app. in the admin interface, it appears on the screen as "Activitys". how can I override the label on the admin page to make it "Activities" instead?
I see in the archives how to do this for a field, but not for a model itself. thanks!
class MyModel(models.Model):
# your fields....
class Meta:
verbose_name = 'Activity'
verbose_name_plural = 'Activities'
Related
I'm working on a project developed in Python 2.7 and Django 1.11.
I'm trying to show in admin page two fields passing through a ManyToMany field.
Here the models:
class ModelZero(Model):
# some fields
mtm_field = models.ManyToManyField(to="ModelOne", through="ModelTwo")
class ModelOne(Model):
# some fields
field_1_1 = models.CharField(unique=True, max_length=200)
field_1_2 = models.BooleanField(default=True)
class ModelTwo(Model):
# some fields
field_2_1 = models.ForeignKey('ModelOne', on_delete=models.CASCADE)
field_2_2 = models.BooleanField(default=True)
In the ModelZero admin page I want to show some fields from the ModelZero itself plus field_2_1 and field_2_2 from ModelTwo.
More in detail, the field_2_1 should be present using a custom widget.
Please note that ModelZeroAdmin is an inline ones.
Here the admin page:
class ModelZeroAdmin(DynamicRawIDMixin, admin.TabularInline):
model = ModelZero
fields = ('some', 'fields', 'field_2_2')
form = forms.ModelZeroForm
def field_2_2(self, obj):
return obj.mtm_field.through.field_2_2
Here the form:
class ModelZeroForm(ModelForm):
class Meta:
widgets = {
"mtm_field.through.field_2_1": dal.autocomplete.ModelSelect2Multiple(
url="my-autocomplete-url"
)
}
In this way i have two errors:
it's not possible add custom fields (field_2_2) in the fields tuple
custom widget is not showed
Is there a way to achieve this goal using this models structure?
I don't have experience with older Django version, but if I am not mistaken the syntax for a related field in admin interface would be something like :mtm_field__field_2_2.
I can't seem to work out how to hook into the queryset of a readonly field in Django admin. In particular I want to do this for an inline admin.
# models.py
class Value(models.Model):
name = models.TextField()
class AnotherModel(models.Model):
values = models.ManyToManyField(Value)
class Model(models.Model):
another_model = models.ForeignKey(AnotherModel)
# admin.py
class AnotherModelInline(admin.TabularInline):
# How do I order values by 'name'?
readonly_fields = ('values',)
class ModelAdmin(admin.ModelAdmin):
inlines = (AnotherModelInline,)
Note that this could probably be done by overriding the form and then setting the widget to disabled, but that's a bit of a hack and doesn't look nice (I don't want greyed out multi-select, but a comma-separated list of words.
You can set an ordering metadata in the Values model:
class Value(models.Model):
name = models.TextField()
class Meta:
ordering = ['name']
I have a Profiles app that has a model called profile, i use that model to extend the django built in user model without subclassing it.
models.py
class BaseProfile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, related_name='owner',primary_key=True)
supervisor = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='supervisor', null=True, blank=True)
#python_2_unicode_compatible
class Profile(BaseProfile):
def __str__(self):
return "{}'s profile". format(self.user)
admin.py
class UserProfileInline(admin.StackedInline):
model = Profile
class NewUserAdmin(NamedUserAdmin):
inlines = [UserProfileInline ]
admin.site.unregister(User)
admin.site.register(User, NewUserAdmin)
admin
the error is
<class 'profiles.admin.UserProfileInline'>: (admin.E202) 'profiles.Profile' has more than one ForeignKey to 'authtools.User'.
obviously i want to select a user to be a supervisor to another user. I think the relationship in the model is OK, the one that's complaining is admins.py file. Any idea ?
You need to use multiple inline admin.
When you have a model with multiple ForeignKeys to the same parent model, you'll need specify the fk_name attribute in your inline admin:
class UserProfileInline(admin.StackedInline):
model = Profile
fk_name = "user"
class SupervisorProfileInline(admin.StackedInline):
model = Profile
fk_name = "supervisor"
class NewUserAdmin(NamedUserAdmin):
inlines = [UserProfileInline, SupervisorProfileInline]
Django has some documentation on dealing with this: https://docs.djangoproject.com/en/1.9/ref/contrib/admin/#working-with-a-model-with-two-or-more-foreign-keys-to-the-same-parent-model
Here is an example that I have just tested to be working
class Task(models.Model):
owner = models.ForeignKey(User, related_name='task_owner')
assignee = models.ForeignKey(User, related_name='task_assigned_to')
In admin.py
class TaskInLine(admin.TabularInLine):
model = User
#admin.register(Task)
class MyModelAdmin(admin.ModelAdmin):
list_display = ['owner', 'assignee']
inlines = [TaskInLine]
Is there any way to use a Form Wizard in Admin interface for add/edit Models.
(using Django 1.5.2)
for example:
--models.py--
class AModel(models.Model):
fieldA = models.CharField(max_length=64)
fieldB = models.CharField(max_length=64)
--admin.py--
class Form1(ModelForm):
class Meta:
model = AModel
fields = ('fieldA',)
class Form2( ModelForm ):
class Meta:
model = AModel
fields = ('fieldB',)
.... something add for make this two forms in one multipage admin form , is that possible? or any other way to do the same job.
Thanks in advance.
It isn't possible by using admin module. The only way is either some external plugins or you need to create custom admin views. Google around..
Given:
#models.py
from django.db import models
class MyModel(models.Model):
myfield = models.IntegerField('Detailed description')
#forms.py
from django import forms
from models import MyModel
class MyModelForm(forms.ModelForm):
myfield = forms.IntegerField()
class Meta:
model = MyModel
With this setup - verbose_name will not be shown on admin page, name of field will be instead (with capitalized first letter). If remove overriding field from MyModelForm - verbose name will be shown as usual. If provide string with description as argument label, forms.IntegerField() - it will be displayed on admin page.
Im looking for pythonic way to display verbose_name defined in model class in case if this field form is overrided in ModelForm, without using label hint.
Any thoughts?
You can access the verbose name of the model in the form like this:
myfield = models.IntegerField(label=MyModel._meta.get_field_by_name('myfield')[0].verbose_name)