反向访问器模型冲突Django Web App

3htmauhk  于 2023-03-20  发布在  Go
关注(0)|答案(1)|浏览(126)
from django.db import models
from django.contrib.auth.models import AbstractUser

class Customer(AbstractUser):
    address_line = models.CharField(max_length=255)
    city = models.CharField(max_length=255)
    postal_code = models.CharField(max_length=10)
    phone_number = models.CharField(max_length=20)
    origin_country = models.CharField(max_length=255)
    profile_picture = models.ImageField(upload_to='customer_profile_pics', null=True, blank=True)

class Chef(AbstractUser):
    address_line = models.CharField(max_length=255)
    city = models.CharField(max_length=255)
    postal_code = models.CharField(max_length=10)
    phone_number = models.CharField(max_length=20)
    origin_country = models.CharField(max_length=255)
    profile_picture = models.ImageField(upload_to='chef_profile_pics', null=True, blank=True)

class Meal(models.Model):
    name = models.CharField(max_length=255)
    description = models.TextField()
    picture = models.ImageField(upload_to='meal_pics')
    country = models.CharField(max_length=255)
    chef = models.ForeignKey(Chef, on_delete=models.CASCADE, related_name='meals')
    customers = models.ManyToManyField(Customer, blank=True, related_name='meals')

class Discussion(models.Model):
    message = models.TextField()
    timestamp = models.DateTimeField(auto_now_add=True)
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE, null=True, blank=True, related_name='discussions')
    chef = models.ForeignKey(Chef, on_delete=models.CASCADE, null=True, blank=True, related_name='discussions')

这是我的models.py文件,在进行迁移时出现以下错误

SystemCheckError: System check identified some issues:

ERRORS:
meals.Chef.groups: (fields.E304) Reverse accessor for 'Chef.groups' clashes with reverse accessor for 'Customer.groups'.
        HINT: Add or change a related_name argument to the definition for 'Chef.groups' or 'Customer.groups'.
meals.Chef.user_permissions: (fields.E304) Reverse accessor for 'Chef.user_permissions' clashes with reverse accessor for 'Customer.user_permissions'.
        HINT: Add or change a related_name argument to the definition for 'Chef.user_permissions' or 'Customer.user_permissions'.
meals.Customer.groups: (fields.E304) Reverse accessor for 'Customer.groups' clashes with reverse accessor for 'Chef.groups'.
        HINT: Add or change a related_name argument to the definition for 'Customer.groups' or 'Chef.groups'.
meals.Customer.user_permissions: (fields.E304) Reverse accessor for 'Customer.user_permissions' clashes with reverse accessor for 'Chef.user_permissions'.
        HINT: Add or change a related_name argument to the definition for 'Customer.user_permissions' or 'Chef.user_permissions'.

我尝试了一些解决方案在互联网上,但它没有工作,我想测试我的浏览器上的应用程序,但使迁移不断显示我这个错误,请帮助!!!
我真的是Django后端开发的新手,我试着解决这个问题,但我做不到。

y1aodyip

y1aodyip1#

为什么要创建两个用户模型?使用相同的字段?AbstractUser是用来定义用于验证的自定义模型。您确定您已经理解了吗?
为了解决您的问题,您必须覆盖Abstract user中的user_permissionsgroups,以便重新定义此字段的related_name ...

class Chef(...):
    groups = models.ManyToManyField(
        Group,
        related_name="XXXXX",
        related_query_name="user",
    )
    user_permissions = models.ManyToManyField(
        Permission,
        related_name="YYYYY",
        related_query_name="user",
    )

相关问题