I have two models in my Django app file models.py
like so:
class User(models.Model):
user = models.IntegerField(primary_key=True,max_length=5)
first_name = models.CharField(max_length=35)
last_name = models.CharField(max_length=35)
class Device(models.Model):
device_name = models.CharField(unique=True,max_length=30)
user = models.ForeignKey('User')
Each Device
can only have one User
, but each User
can have more than one Device
.
In the admin interface, when I display a Device
it shows the User
in a drop-down list, but when I display the User
, I don't see the associated Device
. How do I fix this so I can see the relationship from either side?
You need to use InlineModelAdmin objects
UPD
Should look like:
class DeviceInline(admin.TabularInline):
model = Device
class UserAdmin(admin.ModelAdmin):
inlines = [
DeviceInline
]
You can use StackedInline with extra=0 option.
class DeviceInline(admin.StackedInline):
model = User
extra = 0
def has_add_permission(request):
return True or False # change for your context
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.