Django反向访问冲突

Django反向访问冲突

本文介绍了Django反向访问冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以在这里我有很多问题与反向访问者的冲突。我虽然很聪明,但我的模型结构是可以使用user.profile来进行任何种类的配置文件,也可以使用provider.profile作为任何一个提供者,但是我在整个地方都有冲突错误。



请注意看看我的代码,看看是否有任何方法来对付这个,所以我仍然可以只使用user.profile,而不需要知道哪种的配置文件(同样是provider.profile,而不需要知道哪种提供程序)?或者只是告诉我,我想要做的是不可能和任何更好的解决方案?

  class BaseProfileModel(models.Model) :
'''
包含与所有用户相关的字段和/或方法的抽象模型类。
'''
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(
v erbose_name = _(关于),
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})

#方法

class BaseHumanProfileModel(BaseProfileModel) :
'''
包含与人类用户相关的字段的抽象基类
'''
birth_date = models.DateField(verbose_name = _(出生日期))
GENDER_CHOICES =(
('M',_('Male')),
('F',_('女性)),
('N' ('未指定'))

gender = models.CharField(
max_length = 1,choices = GENDER_CHOICES,default ='N',verbose_name = _('Gender'))

class Meta:
abstract = True

class CustomerProfile(BaseHumanProfileModel):
'''
具体消费者的人类子类
'''
home_location = models.OneToOneField(
位置,
related_name ='customer',
null = True,
blank = True,
on_delete = models.SET_NULL

profile_url_name ='profiles:customer_profile'

#方法

类提供者(models.Model):
'''
包含提供商所需的信息
其他模型(提供者个人资料,评论,活动等)将使用此类
作为ForeignKey来与创建和管理
会话/列表相关的功能进行交互。
'''
stripe_access_token = models.TextField(blank = True,default ='')

#方法....


class IndividualProviderProfile(BaseHumanProfileModel):
'''
表示单个提供程序的配置文件的具体子类。
''
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,

认证= models.CharField(
verbose_name = _(Certifications),
max_length = 200,
blank = True,

profile_url_name ='profiles:individual_provider_profile'

#方法


class OrganizationProviderProfile(BaseProfileModel):
'''
表示作为组织的提供程序的配置文件。
包含用于与会话调度进行连接的提供程序类的键
还包含为组织工作的单个提供程序的集合。
'''
provider = models.OneToOneField(Provider,related_name ='profile')
website = models.URLField(blank = True)
location = models.ForeignKey ,related_name ='organization')
employees = models.ManyToManyField(IndividualProviderProfile,null = True,blank = True,related_name ='organization')

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

profile_url_name ='个人资料:organization_provider_profile'

#methods

现在,当我尝试同步db时,我得到这个相当长而强烈的警告:

  CommandError:系统检查确定了一些问题:

错误:
profiles.CustomerProfile.user:(fields。 E304)CustomerProfile.user的反向访问器与OrganizationProviderProfile.user的反向访问器冲突。
提示:将相关名称参数添加或更改为CustomerProfile.user或OrganizationProfile.user的定义。
profiles.CustomerProfile.user:(fields.E304)'CustomerProfile.user'的反向访问者与IndividualProviderProfile.user的反向访问器冲突。
提示:添加或更改一个related_name参数到CustomerProfile.user或IndividualProviderProfile.user的定义。
profiles.CustomerProfile.user:(fields.E305)'CustomerProfile.user'的反向查询名与OrganizationProviderProfile.user的反向查询名冲突。
提示:添加或更改一个related_name参数到CustomerProfile.user或OrganizationProviderProfile.user的定义。
profiles.CustomerProfile.user:(fields.E305)'CustomerProfile.user'的反向查询名称与IndividualProviderProfile.user的反向查询名冲突。
提示:添加或更改一个related_name参数到CustomerProfile.user或IndividualProviderProfile.user的定义。
profiles.OrganizationProfile.provider:(fields.E304)'OrganizationProviderProfile.provider'的反向访问器与IndividualProviderProfile.provider的反向访问器冲突。
提示:添加或更改一个related_name参数到'OrganizationProviderProfile.provider'或'IndividualProviderProfile.provider'的定义。
profiles.OrganizationProviderProfile.provider:(fields.E305)OrganizationProviderProfile.provider的反向查询名称与IndividualProviderProfile.provider的反向查询名称冲突。
提示:添加或更改一个related_name参数到'OrganizationProviderProfile.provider'或'IndividualProviderProfile.provider'的定义。
profiles.OrganizationProviderProfile.user:(fields.E304)'OrganizationProviderProfile.user'的反向访问器与CustomerProfile.user的反向访问器冲突。
提示:在OrganizationProviderProfile.user或CustomerProfile.user的定义中添加或更改related_name参数。
profiles.OrganizationProviderProfile.user:(fields.E304)OrganizationProviderProfile.user的反向访问者与IndividualProviderProfile.user的反向访问器冲突。
提示:在OrganizationProviderProfile.user或IndividualProvider.user的定义中添加或更改related_name参数。
profiles.OrganizationProviderProfile.user:(fields.E305)OrganizationProviderProfile.user的反向查询名称与CustomerProfile.user的反向查询名称冲突。
提示:在OrganizationProviderProfile.user或CustomerProfile.user的定义中添加或更改related_name参数。
profiles.OrganizationProviderProfile.user:(fields.E305)OrganizationProviderProfile.user的反向查询名称与IndividualProviderProfile.user的反向查询名称冲突。
提示:在OrganizationProviderProfile.user或IndividualProvider.user的定义中添加或更改related_name参数。
profiles.IndividualProviderProfile.provider:(fields.E304)'IndividualProviderProfile.provider'的反向访问者与OrganizationProviderProfile.provider的反向访问器冲突。
提示:添加或更改related_name参数到'IndividualProviderProfile.provider'或'OrganizationProviderProfile.provider'的定义。
profiles.IndividualProviderProfile.provider:(fields.E305)'IndividualProviderProfile.provider'的反向查询名与OrganizationProviderProfile.provider的反向查询名冲突。
提示:添加或更改related_name参数到'IndividualProviderProfile.provider'或'OrganizationProviderProfile.provider'的定义。
profiles.IndividualProviderProfile.user:(fields.E304)'IndividualProviderProfile.user'的反向访问器与CustomerProfile.user的反向访问器冲突。
提示:将related_name参数添加或更改为IndividualProviderProfile.user或CustomerProfile.user的定义。
profiles.IndividualProviderProfile.user:(fields.E304)'IndividualProviderProfile.user'的反向访问器与OrganizationProviderProfile.user的反向访问器冲突。
提示:添加或更改一个related_name参数到'IndividualProviderProfile.user'或'OrganizationProviderProfile.user'的定义。
profiles.IndividualProviderProfile.user:(fields.E305)'IndividualProviderProfile.user'的反向查询名与CustomerProfile.user的反向查询名冲突。
提示:将related_name参数添加或更改为IndividualProviderProfile.user或CustomerProfile.user的定义。
profiles.IndividualProviderProfile.user:(fields.E305)'IndividualProviderProfile.user'的反向查询名称与OrganizationProviderProfile.user的反向查询名冲突。
提示:添加或更改一个related_name参数到'IndividualProviderProfile.user'或'OrganizationProviderProfile.user'的定义。


解决方案

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



尝试重命名为: / p>

  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')


So here I'm having lots of problems with reverse accessor clashes. I though I was being pretty clever and DRY with my model structure to be able to use user.profile for any kind of profile, or to use provider.profile for either provider... but I'm getting clash errors all over the place.

Mind taking a look at my code and seeing if there is any way for me to finagle this so I can still user just the user.profile without needing to know which kind of profile (and likewise provider.profile without needing to know which kind of provider)? Or just telling me that what I'm trying to do is impossible and any better solutions?

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

Now when I try to sync the db I get this rather long and intense set of warnings:

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's must be unique. You're giving the same name to all the related_name's.

Try to rename them like this:

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反向访问冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 20:03