Django:模型验证错误ManytoManyField

发布于 2024-11-06 17:17:32 字数 1646 浏览 0 评论 0原文

我在运行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

I get this error while running syncdb

Can't seem to figure out the issue. Please help.

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

Here are my models:

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)

Store Model:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟雨扶苏 2024-11-13 17:17:32

尝试执行以下操作:

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

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

Try doing something like this:

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

Also, if that doesn't work, try changing the related_name on the Business.logo field to something not logo_id. logo_id is what the database uses for the field and it may be having a conflict.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文