I have a model with start_date
and end_date
. I would like to add a list of users at the bottom so that an admin can pick from a list of users that are associated with this model.
This is how the model looks in admin panel at the moment
My model looks like this in models.py
class MyPeriod(ValidateOnSaveMixin, models.Model):
start_date = models.DateField(unique=True)
end_date = models.DateField()
In admin.py
I tried adding filter_horizontal
like this but it gave me errors
class MyPeriodAdmin(admin.ModelAdmin):
list_display = ('start_date', 'end_date',)
filter_horizontal = ('user',)
The value of 'filter_horizontal[0]' refers to 'user', which is not an attribute of 'MyPeriod'.
Your current model does not contain an association between period and users. You have to specify a ForeignKey relation with the User
model, such as:
from django.conf import settings
...
class MyPeriod(ValidateOnSaveMixin, models.Model):
start_date = models.DateField(unique=True)
end_date = models.DateField()
user = models.ForeignKey(settings.AUTH_USER_MODEL)
After this addition (and applying migrations to reflect the changes to the actual database) you will be able to assign User
s to your MyPeriod
model.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.