简体   繁体   English

Django一对多创建关系

[英]Django creating relation one-to-many

I started learning Django recently, and can't find answer for a simple question. 我最近开始学习Django,但找不到一个简单问题的答案。 I have 2 tables: Client and Addres. 我有2个表:Client和Addres。

------------------
CLIENT |
------------------
ID |
NAME |
ADDRES_REF |
------------------

------------------
ADDRES |
------------------
ID |
NAME |
CITY |
COLLECTION |
------------------

The relation between them is: client.addres_ref=addres.collection. 它们之间的关系是:client.addres_ref = addres.collection。 In order to select all addresses of client with ID equal 123 I have to create such query: 为了选择ID等于123的客户端的所有地址,我必须创建以下查询:

select addres.name, addres.city from addres, client where client.addres_ref=addres.collection and client.id=123;

Certainly its posible to create relation many-to-many, but I dont wont create additional table for it and change the structure of tables. 当然,它可以创建多对多关系,但我不会为它创建其他表并更改表的结构。

class Addres(models.Model):       
    address = models.CharField(max_length=150)
    city    = models.ForeignKey(City)




class Client(models.Model):
    addres          =models.ManyToMany(Addres)        
    email           =models.EmailField(blank=True)
    name            =models.CharField(max_length=50)

It is posible to add ForeignKey(Client) in Addres model, but I need reference to Addres from another models too, like User, Employer ... Help me please to create models with relations from above-stated tables. 可以在Addres模型中添加ForeignKey(Client),但是我也需要从其他模型(例如User,Employer)中引用Addres。请帮助我从上述表中创建具有关系的模型。

This could be helpfull - One to many . 这可能会有所帮助- 一对多 It's an official documentation, so nothing special... 这是官方文档,所以没什么特别的...

I'm not sure if I understood question correctly, but what if you will define collection field as foreign key to Client? 我不确定我是否正确理解了问题,但是如果您将集合字段定义为Client的外键怎么办? Then you should be able to do something like 那你应该可以做类似的事情

address = Address.objects.get(collection=123)

I think what you need to do is use the content types framework to create generic foreign key in the Addres model. 我认为您需要做的是使用内容类型框架在Addres模型中创建通用外键。

from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic

class Addres(models.Model):
    content_type = models.ForeignKey(ContentType, related_name='addresses')
    object_id = models.IntegerField()
    content_object = generic.GenericForeignKey( )

    address = models.CharField()
    city = models.ForeignKey(City)

class Client(models.Model):
    addresses = generic.GenericRelation( Addres )
    email = models.EmailField()
    name = models.CharField()

then client objects will return addresses by using client.addresses or you can query the Addres model like this: 然后客户端对象将使用client.addresses返回地址,或者您可以查询Addres模型,如下所示:

client = Client.objects.get(pk=123)
addresses = Addres.objects.filter(content_object=client)

and the Addres model can be linked to other models as well, eg 并且Addres模型也可以链接到其他模型,例如

class User(models.Model):
     addresses = generic.GenericRelation( Addres )
     name = models.CharField()

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

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