简体   繁体   English

Django:从父表到子表的外键

[英]Django: Foreign key from parent to child table

I want to have a foreign key link from Parent table to child table ; 我想从父表到子表有一个外键链接; for a site which is mainly backend powered. 适用于主要由后端驱动的网站。 Currently only admin part of site is active as that is the only part required to get our information team working. 目前只有站点的管理员部分处于活动状态,因为这是使我们的信息团队工作所需的唯一部分。

App structure: 应用程序结构:

attraction: 吸引力:

| |
|--- model | ---模型
| | | |
| | | | --- _ _init__.py --- _ _init__.py
| | | | --- imagemetadata.py -imagemetadata.py
| |
| | --- models.py --- models.py
| | --- admin.py --- admin.py

(imagemetadata is under directory model) (imagemetadata在目录模型下)

file imagemetadata.py 文件imagemetadata.py

from attraction.models import Attraction

class ImageMetadata(models.Model):
    image = models.ImageField(upload_to='', blank=True, null=True)
    upload_time = models.DateTimeField(null=True)

    attraction = models.ForeignKey(Attraction)

file models.py 文件models.py

from django.db import models

class Attraction(models.Model):
    name = models.CharField(max_length=100, null=False)
    description = models.CharField(max_length=500, null=True)
    url = models.URLField(max_length=200, null=True)

file admin.py 文件admin.py

class ImageMetadataInline(admin.TabularInline):
    model = ImageMetadata
    extra = 2

class AttractionAdmin(admin.ModelAdmin):
    fieldsets = [
        (None, {'fields': ['name', 'description', 'url']}),
    ]

    inlines = [ImageMetadataInline, PriceInline]

admin.site.register(Attraction, AttractionAdmin)

The problem i am facing is i cant get a foreign key from Attraction to Imagemetadata. 我面临的问题是我无法从Attraction到Imagemetadata获得外键。 If I try to make a two way foreign key import error occurs. 如果我尝试进行双向外键导入错误。 ( Cyclic imports not allowed in Python). (Python中不允许循环导入)。 And because of the structure of admin , there cant be a foreign key just from Attraction to ImageMetadata, as Django doesnt allow that. 并且由于admin的结构,从Attraction到ImageMetadata不能有一个外键,因为Django不允许这样做。

Is there a way to make the foreign key point from Attraction to ImageMetadata, without changing the structure of admin ? 有没有办法在不更改admin的结构的情况下将外键从Attraction变为ImageMetadata?

You can define the class Meta as following: 您可以按如下方式定义Meta类:

class ImageMetadata(models.Model):
    # ... your fields

    class Meta:
        app_label = 'attraction'

Make a sub-directory models in your app directory. 在您的应用目录中创建一个子目录models Split your models into different files. 将模型拆分为不同的文件。 Then edit the __init__.py : 然后编辑__init__.py

from model_file1 import *
from model_fiel2 import *
# and so on

Now you can make: 现在您可以进行以下操作:

from attraction.models import whatever

I split in this way my tests. 我以这种方式进行测试。 It should work for the models too. 它也应该适用于模型。

However I would suggest you first to rethink your design. 但是,我建议您首先重新考虑您的设计。 In Python is absolutely all right to have many classes in one file. 在Python中,绝对可以在一个文件中包含多个类。 Python is neither PHP nor Java. Python既不是PHP也不是Java。 200 LOC per file is nothing. 每个文件200 LOC是什么。 I would say it is absolutely okay if your models.py are up to 2k LOC. 我想说,如果您的models.py达到2k LOC,那绝对可以。 Just my personal opinion. 只是我个人的意见。 Maybe some Python gurus here can correct me. 也许这里的一些Python专家可以纠正我。

Before you split your models.py consider if you can maybe split your project in smaller apps. 在拆分models.py之前,请考虑是否可以在较小的应用程序中拆分项目。 Maybe ImageMetadata should belong to another app. 也许ImageMetadata应该属于另一个应用程序。 You can't achieve granularity by splitting the models.py into many files. 通过将models.py拆分为多个文件,您无法获得粒度。 Just because you have many smaller files instead of one big file doesn't mean the project logic is split. 仅仅因为您有许多较小的文件而不是一个大文件,并不意味着项目逻辑已分裂。 You should break down your logic into few self-containing apps. 您应该将逻辑分解为几个自包含的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM