gpt4 book ai didi

django - Django:模型验证错误ManytoManyField

转载 作者:行者123 更新时间:2023-12-02 04:09:36 25 4
gpt4 key购买 nike

运行syncdb时出现此错误

似乎无法找出问题所在。请帮忙。

    Error: One or more models did not validate:
store.business: Reverse query name for field 'logo' clashes with field 'ImageBank.business'. Add a related_name argument to the definition for 'logo'.

这是我的模型:
class Business(models.Model):
business_type = models.ManyToManyField(BusinessType)
business_service_type = models.ManyToManyField(ServiceType)
establishment_type = models.ForeignKey(EstablishmentType)
logo = models.ForeignKey(ImageBank, related_name = '%(class)s_logocreated',)
phone = PhoneNumberField()
address = models.ForeignKey(Address)
website = models.URLField()
name = models.CharField(max_length=64)

def __unicode__(self):
return self.name

class ImageBank(models.Model):
business = models.ForeignKey('Business', related_name='%(class)s_business')
image = models.ImageField(upload_to="images/bank")

def url(self):
return self.image.url

def __unicode__(self):
return unicode(self.business) + " : " + unicode(self.image)

店铺型号:
class Store(models.Model):
business = models.ForeignKey(Business,null=True, related_name='business_creator_set')
condition = models.CharField(verbose_name='What do customers have to do?',max_length = 50)
reward = models.CharField(verbose_name='What do customers win?',max_length = 50)
display = models.BooleanField(default=True)
date_created = models.DateTimeField(default=datetime.now)


def __unicode__(self):
return self.condition + ", " + self.reward

最佳答案

尝试做这样的事情:

...
class ImageBank(models.Model):
business = models.ForeignKey('Business', related_name='%(class)s_business')
....

另外,如果这不起作用,请尝试将Business.logo字段上的related_name更改为logo_id以外的名称。 logo_id是数据库用于该字段的内容,可能存在冲突。

关于django - Django:模型验证错误ManytoManyField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5967591/

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