Django ManytoMany未在管理中显示
因为我的模型有很多关系,所以连接应该是双向的,但不是双向的 例如,在Django ManytoMany未在管理中显示,django,django-models,Django,Django Models,因为我的模型有很多关系,所以连接应该是双向的,但不是双向的 例如,在BusinessProfile的管理部分,我可以看到服务模型的名称和图像字段,因为它们处于多个关系中,但情况并非相反。我无法在管理中的Servicesmodel中查看BusinessProfilemodels字段 我的模型结构正确吗 我还附上了图片。 models.py class Service(models.Model): name = models.CharField(max_length=50) ima
BusinessProfile
的管理部分,我可以看到服务
模型的名称和图像字段,因为它们处于多个关系中,但情况并非相反。我无法在管理中的Services
model中查看BusinessProfile
models字段
我的模型结构正确吗
我还附上了图片。
models.py
class Service(models.Model):
name = models.CharField(max_length=50)
image = models.ImageField(upload_to='image', blank = True)
#business_profile = models.ManyToManyField("BusinessProfile", blank=True, related_name="business_of_services")
def __str__(self):
return "Service - {}".format(self.name)
class BusinessProfile(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, on_delete=models.CASCADE)
business_name = models.CharField(max_length=100, unique =True)
register_date = models.DateTimeField(default=timezone.now)
pan_number = models.IntegerField(unique=True)
pan_name = models.CharField(max_length=100)
address = models.TextField(max_length=200)
pincode = models.IntegerField()
city = models.CharField(max_length=50)
state = models.CharField(max_length=50)
service = models.ManyToManyField("Service", blank=True, related_name="services_of_business")
image = models.ManyToManyField("Service", related_name="image_of_business")
def __str__(self):
return "Business - {}".format(self.business_name)
如果要使用反向多对多管理,可以使用内联线。看 文档中的示例代码
from django.contrib import admin
class MembershipInline(admin.TabularInline):
model = Group.members.through
class PersonAdmin(admin.ModelAdmin):
inlines = [
MembershipInline,
]
class GroupAdmin(admin.ModelAdmin):
inlines = [
MembershipInline,
]
exclude = ('members',)