Django 1.11多对多不出现在Django管理中
嗨,我有一个django通知模型,它有一个多对多关系,但django管理中没有显示任何内容(所有字段都没有显示) 国家/地区和设备代码Django 1.11多对多不出现在Django管理中,django,python-3.x,Django,Python 3.x,嗨,我有一个django通知模型,它有一个多对多关系,但django管理中没有显示任何内容(所有字段都没有显示) 国家/地区和设备代码 class Device(models.Model): """Store device related to :model:`accounts.User`.""" user = models.OneToOneField(User, related_name='device', on_delete=models.CASCADE) mod
class Device(models.Model):
"""Store device related to :model:`accounts.User`."""
user = models.OneToOneField(User, related_name='device', on_delete=models.CASCADE)
model = models.CharField(max_length=200, blank=True, null=True)
player_id = models.CharField(max_length=200, blank=True, null=True)
class Meta:
verbose_name = 'Device'
verbose_name_plural = 'Devices'
def __str__(self):
return self.model
class Country(models.Model):
name = models.CharField(max_length=255)
def __str__(self):
return self.name
管理员
from django.contrib import admin
from .models import Notification
admin.site.register(Notification)
编辑:
谢谢你所有的问题都解决了
该问题是由于设备型号中的某些条目在型号字段中没有,因此正确显示该条目时出现问题。根据:
当一个类有一个字段没有显示在管理界面中,但是
不能为空,无法添加新的。你得到一个神秘的
“请更正下面的错误。”没有显示错误的消息。这个
错误消息可能会显示一些关于隐藏字段的信息
现在许多字段不需要null=True,请尝试删除这些语句,看看是否有改进
另外,尝试在admin.py中添加国家/地区和设备型号,以便管理员可以查看并显示它们。
为admin.py中的多对多定义内联:
from django.contrib import admin
class DeviceInline(admin.TabularInline):
model = Notification.device.through
class CountryInline(admin.TabularInline):
model = Notification.country.through
class NotificationAdmin(admin.ModelAdmin):
inlines = [
DeviceInline, CountryInline
]
exclude = ("device", "country")
你能为设备和国家/地区型号添加代码吗?显示你的admin.py文件。感谢你的帮助,我尝试了你的解决方案,但没有任何改变。如果你将国家/地区和设备添加到admin.py,会发生什么?由于您有许多模型,要添加这些模型,管理员可能需要访问这些模型
from django.contrib import admin
class DeviceInline(admin.TabularInline):
model = Notification.device.through
class CountryInline(admin.TabularInline):
model = Notification.country.through
class NotificationAdmin(admin.ModelAdmin):
inlines = [
DeviceInline, CountryInline
]
exclude = ("device", "country")