gpt4 book ai didi

Django 反向访问器冲突

转载 作者:行者123 更新时间:2023-11-28 19:35:00 25 4
gpt4 key购买 nike

所以在这里我遇到了很多反向访问器冲突的问题。我虽然非常聪明并且对我的模型结构很干,能够将 user.profile 用于任何类型的配置文件,或者将 provider.profile 用于任一提供者......但我到处都是冲突错误.

介意看看我的代码,看看我是否有任何方法可以解决这个问题,这样我仍然可以只使用 user.profile 而无需知道哪种配置文件(同样不需要知道 provider.profile哪种供应商)?或者只是告诉我我正在尝试做的事情是不可能的,还有更好的解决方案吗?

class BaseProfileModel(models.Model):
'''
An abstract model class containing fields and/or methods relevant to all users.
'''
user = models.OneToOneField(User, related_name="profile", primary_key=True)
phone = PhoneNumberField(verbose_name=_("Phone Number"))
pic = models.ImageField(upload_to=get_upload_file_name,
width_field="width_field",
height_field="height_field",
null=True,
blank=True,
verbose_name=_("Profile Picture")
)
height_field = models.PositiveIntegerField(null=True, default=0)
width_field = models.PositiveIntegerField(null=True, default=0)
thumbnail = ImageSpecField(source='pic',
processors=[ResizeToFill(180,180)],
format='JPEG',
options={'quality': 100})
bio = models.TextField(
verbose_name=_("About"),
default="",
blank=True,
max_length=800
)

class Meta:
abstract = True

def __str__(self):
if self.user.email:
return self.user.email
else:
return self.user.username

@property
def is_provider(self):
return hasattr(self, 'provider')

def get_absolute_url(self):
return reverse_lazy(self.profile_url_name, kwargs={'pk': self.pk})

# Methods

class BaseHumanProfileModel(BaseProfileModel):
'''
Abstract base class containing fields relevant to human users
'''
birth_date = models.DateField(verbose_name=_("Date of Birth"))
GENDER_CHOICES = (
('M', _('Male')),
('F', _('Female')),
('N', _('Not Specified')),
)
gender = models.CharField(
max_length=1, choices=GENDER_CHOICES, default='N', verbose_name=_('Gender'))

class Meta:
abstract = True

class CustomerProfile(BaseHumanProfileModel):
'''
Concrete Human subclass for the consumers
'''
home_location = models.OneToOneField(
Location,
related_name='customer',
null=True,
blank=True,
on_delete=models.SET_NULL
)
profile_url_name = 'profiles:customer_profile'

# Methods

class Provider(models.Model):
'''
Class containing information needed for providers
Other models (provider profiles, reviews, events, etc.) will use this class
as ForeignKey to interface with functionalities related to creating and managing
sessions/listings.
'''
stripe_access_token = models.TextField(blank=True, default='')

# Methods....


class IndividualProviderProfile(BaseHumanProfileModel):
'''
Concrete subclass for representing the profile of an individual provider.
'''
provider = models.OneToOneField(Provider, related_name='profile')
locations = models.ManyToManyField(Location, null=True, blank=True, related_name='individual_providers')
specialties = models.CharField(
verbose_name=_("Specialties"),
max_length=200,
blank=True,
)
certifications = models.CharField(
verbose_name=_("Certifications"),
max_length=200,
blank=True,
)
profile_url_name = 'profiles:individual_provider_profile'

# methods


class OrganizationProviderProfile(BaseProfileModel):
'''
Profile representing a provider that is an organization.
Contains key to provider class for interfacing with session scheduling
Also contains set of individual providers that work for the organization.
'''
provider = models.OneToOneField(Provider, related_name='profile')
website = models.URLField(blank=True)
location = models.ForeignKey(Location, related_name='organization')
employees = models.ManyToManyField(IndividualProviderProfile, null=True, blank=True, related_name='organization')

@property
def locations(self):
return Locations.objects.filter(pk=self.location.pk)

profile_url_name = 'profiles:organization_provider_profile'

#methods

现在,当我尝试同步数据库时,我收到了一组相当长且强烈的警告:

CommandError: System check identified some issues:

ERRORS:
profiles.CustomerProfile.user: (fields.E304) Reverse accessor for 'CustomerProfile.user' clashes with reverse accessor for 'OrganizationProviderProfile.user'.
HINT: Add or change a related_name argument to the definition for 'CustomerProfile.user' or 'OrganizationProfile.user'.
profiles.CustomerProfile.user: (fields.E304) Reverse accessor for 'CustomerProfile.user' clashes with reverse accessor for 'IndividualProviderProfile.user'.
HINT: Add or change a related_name argument to the definition for 'CustomerProfile.user' or 'IndividualProviderProfile.user'.
profiles.CustomerProfile.user: (fields.E305) Reverse query name for 'CustomerProfile.user' clashes with reverse query name for 'OrganizationProviderProfile.user'.
HINT: Add or change a related_name argument to the definition for 'CustomerProfile.user' or 'OrganizationProviderProfile.user'.
profiles.CustomerProfile.user: (fields.E305) Reverse query name for 'CustomerProfile.user' clashes with reverse query name for 'IndividualProviderProfile.user'.
HINT: Add or change a related_name argument to the definition for 'CustomerProfile.user' or 'IndividualProviderProfile.user'.
profiles.OrganizationProfile.provider: (fields.E304) Reverse accessor for 'OrganizationProviderProfile.provider' clashes with reverse accessor for 'IndividualProviderProfile.provider'.
HINT: Add or change a related_name argument to the definition for 'OrganizationProviderProfile.provider' or 'IndividualProviderProfile.provider'.
profiles.OrganizationProviderProfile.provider: (fields.E305) Reverse query name for 'OrganizationProviderProfile.provider' clashes with reverse query name for 'IndividualProviderProfile.provider'.
HINT: Add or change a related_name argument to the definition for 'OrganizationProviderProfile.provider' or 'IndividualProviderProfile.provider'.
profiles.OrganizationProviderProfile.user: (fields.E304) Reverse accessor for 'OrganizationProviderProfile.user' clashes with reverse accessor for 'CustomerProfile.user'.
HINT: Add or change a related_name argument to the definition for 'OrganizationProviderProfile.user' or 'CustomerProfile.user'.
profiles.OrganizationProviderProfile.user: (fields.E304) Reverse accessor for 'OrganizationProviderProfile.user' clashes with reverse accessor for 'IndividualProviderProfile.user'.
HINT: Add or change a related_name argument to the definition for 'OrganizationProviderProfile.user' or 'IndividualProvider.user'.
profiles.OrganizationProviderProfile.user: (fields.E305) Reverse query name for 'OrganizationProviderProfile.user' clashes with reverse query name for 'CustomerProfile.user'.
HINT: Add or change a related_name argument to the definition for 'OrganizationProviderProfile.user' or 'CustomerProfile.user'.
profiles.OrganizationProviderProfile.user: (fields.E305) Reverse query name for 'OrganizationProviderProfile.user' clashes with reverse query name for 'IndividualProviderProfile.user'.
HINT: Add or change a related_name argument to the definition for 'OrganizationProviderProfile.user' or 'IndividualProvider.user'.
profiles.IndividualProviderProfile.provider: (fields.E304) Reverse accessor for 'IndividualProviderProfile.provider' clashes with reverse accessor for 'OrganizationProviderProfile.provider'.
HINT: Add or change a related_name argument to the definition for 'IndividualProviderProfile.provider' or 'OrganizationProviderProfile.provider'.
profiles.IndividualProviderProfile.provider: (fields.E305) Reverse query name for 'IndividualProviderProfile.provider' clashes with reverse query name for 'OrganizationProviderProfile.provider'.
HINT: Add or change a related_name argument to the definition for 'IndividualProviderProfile.provider' or 'OrganizationProviderProfile.provider'.
profiles.IndividualProviderProfile.user: (fields.E304) Reverse accessor for 'IndividualProviderProfile.user' clashes with reverse accessor for 'CustomerProfile.user'.
HINT: Add or change a related_name argument to the definition for 'IndividualProviderProfile.user' or 'CustomerProfile.user'.
profiles.IndividualProviderProfile.user: (fields.E304) Reverse accessor for 'IndividualProviderProfile.user' clashes with reverse accessor for 'OrganizationProviderProfile.user'.
HINT: Add or change a related_name argument to the definition for 'IndividualProviderProfile.user' or 'OrganizationProviderProfile.user'.
profiles.IndividualProviderProfile.user: (fields.E305) Reverse query name for 'IndividualProviderProfile.user' clashes with reverse query name for 'CustomerProfile.user'.
HINT: Add or change a related_name argument to the definition for 'IndividualProviderProfile.user' or 'CustomerProfile.user'.
profiles.IndividualProviderProfile.user: (fields.E305) Reverse query name for 'IndividualProviderProfile.user' clashes with reverse query name for 'OrganizationProviderProfile.user'.
HINT: Add or change a related_name argument to the definition for 'IndividualProviderProfile.user' or 'OrganizationProviderProfile.user'.

最佳答案

related_name 必须是唯一的。您为所有 related_name 赋予相同的名称。

尝试像这样重命名它们:

user = models.OneToOneField(User, related_name="custom_user_profile", primary_key=True)
#..
provider = models.OneToOneField(Provider, related_name='user_ind_provider_profile')
#..
provider = models.OneToOneField(Provider, related_name='user_org_provider_profile')

关于Django 反向访问器冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26955319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com