上课没有外键 [英] class has no foreign key to class

查看:159
本文介绍了上课没有外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的Django Models设置如下:

I've got my Django Models setup as follows:

class Cities(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255, unique=True)

    def __unicode__(self):
        return self.name


class Pincode(models.Model):
    id = models.AutoField(primary_key=True)
    pin = models.CharField(max_length=20, unique=True)

    def __unicode__(self):
        return self.pin


class Address(models.Model):
    id = models.AutoField(primary_key=True)
    build_add = models.CharField(max_length=255)
    street_add = models.CharField(max_length=255)
    area = models.CharField(max_length=255)
    city = models.ForeignKey(Cities)
    pin = models.ForeignKey(Pincode)

    def __unicode__(self):
        return self.area


class Classified(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=256)
    contact_person = models.CharField(max_length=300, blank=True)
    email = models.CharField(max_length=100, blank=True)
    address = models.ForeignKey(Address)
    subcategory = models.ForeignKey(Subcategory)
    area_code = models.CharField(max_length=10, blank=True,  default='')
    phone_number = models.BigIntegerField(max_length=20, default=0, blank=True)
    secondary_number = models.BigIntegerField(max_length=20, default=0, blank=True)
    more_numbers = models.CharField(max_length=300, default='', blank=True)
    image = S3DirectField(upload_to='s3direct', blank=True)
    secondary_image = S3DirectField(upload_to='s3direct', blank=True, default='')
    description = models.TextField(max_length=1000, blank=True)
    location = LocationField(blank=True, max_length=255)
    NO = 'NO'
    YES = 'YES'
    APPROVAL = ((NO, 'no'), (YES, 'yes'))
    active = models.CharField(choices=APPROVAL, default=NO, max_length=3)
    verified = models.CharField(choices=APPROVAL, default=NO, max_length=3)

我的问题与执行内联有关,因为我需要每个数据在分类中的条目在管理员本身的分类视图中进行编辑。

My question is to do with performing inlines as I need data for each entry in Classified to be edited in the Classified view in the admin itself.

我尝试过以下操作来编辑同一个视图中的地址。

I've tried the following to edit the address in the same view

class AddressInline(admin.TabularInline):
    model = Address


class ClassifiedAdmin(admin.ModelAdmin):
    inlines = [AddressInline, ]
    search_fields = [
        'name',
    ]


admin.site.register(Classified, ClassifiedAdmin)

但是,这给了我以下错误:

However, this gives me the following error:

<class 'appname.models.Address'> has no ForeignKey to <class'appname.models.Classified'>

如何解决这个错误?我已经看过并尝试从<类>没有< class>的外键在Django尝试内嵌模型内联Django管理员:有没有ForeignKey ,但他们似乎没有为我工作。

How do I solve this error? I've looked and tried the solutions from <class> has no foreign key to <class> in Django when trying to inline models and Inline in Django admin: has no ForeignKey but they don't seem to be working for me.

推荐答案

你的内联写错了方式也许。内联应该用于依赖模型实例。因此,您可以在地址中分配一个分类,而不是其他方式,因为分类取决于地址与外键关系。如果你有一个外键关系,你可以使用一个常规的字段,而窗体最后会有一个选择框来选择地址。如果你真的想要一个分类的对象在多个地址之间出现,那么使用 ManyToManyField 而不是 ForeignKey ,然后Django将自动为您生成一个中间表。

Your inline is written the wrong way around perhaps. An inline should be used for dependent model instances. So you can have a Classified in an Address, but not the other way around, as Classified depends on Address with a foreign key relation. If you have a foreign key relation, you can just use a regular field instead, and the form will end up with a select box for choosing the address. If you really want one Classified object to appear across multiple addresess, then use a ManyToManyField instead of a ForeignKey, and then Django will generate an intermediate table for you automatically.

此外,您不需要 AutoField 代码中的行。默认情况下,Django将生成一个名称为id的自动递增主键。

Also, you don't need the AutoField lines in your code. Django will generate an auto-incrementing primary key with the attribute name 'id' by default.

这篇关于上课没有外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆