Django管理员Django通用关系 [英] Django Generic Relations with Django Admin

查看:123
本文介绍了Django管理员Django通用关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Django项目,它有一个地址模型。这是在几个地方使用 - 用户个人资料模型,由医院模型,Insitution模型等。



我使用Django的通用关系允许这些对象中的每一个创建一个外键来寻址。



然而,这似乎在Django Admin中引起了一些奇怪(或者也许是不正确地理解它是如何被使用的)。在Django管理员中,如果我尝试创建一个地址,我会看到Content type和Object id的字段。如果没有填写,模型将不会验证/保存。不确定这些内容。



事情是,我想要创建独立的地址对象。然后,当我创建一个用户配置文件或一个医院时,我可以将它们链接到Address对象,包括多个链接到同一个Address对象的可能性。



我应该如何使用Django管理员的通用关系?



此外,我也打算使用django-reversion进行版本控制,不知道这是否会导致通用关系和管理员的任何问题?



干杯,
维克多



编辑:我应该只是添加,这是一个较早的问题我发布地址和内联:



Django - 设计模型关系 - 管理界面和内联



根据给出的答案,为什么地址模型是具有外键的模型。而且由于正常的FK字段只能指向一种类型的对象,所以我们使用通用关系。



每个用户/部门/医院等都可以在大多数情况下会有多个地址。



同一个地址可以被多个实体使用,但是这是很少的,重复在这里很好,我猜,对/?

所以这是一个一对多的用户/部门/医院到地址。



在原始问题中,他们还建议使用抽象类,并为每个需要Address的实体提供不同的Address模型。我仍然不知道这是否是更好的方法,或者如果有一种方法可以使GenericRelations与我在这里尝试做的工作。

解决方案

在django中使用泛型关系就是这样。一个 ForeignKey to ContentType(content_type)和一个 IntegerField 来表示实例id(object_id)。如果您不知道 ForeignKey 指向的内容类型,这些功能很有用。由于您知道您要定位地址模型,您需要使用常规的 ForeignKey(Address)而不是通用关系。



为了回应您的评论



其实更容易使用 ForeignKey ,因为你不需要通过ContentType。

  class Address $ model $模型。$ ​​b $ b street = models.CharField(max_length = 100)
city = models.CharField(max_length = 100)


class医院模型)
name = models.CharField(max_length = 100)
created = models.DateTimeField()
address = models.ForeignKey(Address,related_name =hospital)

class Institution(models.Model):
name = models.CharField(max_length = 100)
address = models.ForeignKey(Address,related_name =institutions)


>>> instance = Institution.objects.get(id = 1)
>>> instance.address.city
>>> address = Address.objects.get(id = 1)
>>> address.institutions.all()
>>> address.hospitals.all()

您的模型是否要共享地址?即可以医院机构可能还有一个 UserProfile 都指向同一个地址实例?还是更有可能每个都有自己的地址?我想了解为什么你创建了一个单独的课程地址。如果要避免将相同的字段重新输入到每个类中,您可以使用抽象模型类并将其子类化。或者您可能需要一个 OneToOneField ,这是两个实例之间的双向指针。




I have a Django project, that has a "Address" model. This is used in several places - by a "User Profile" model, by a "Hospital" model, by an "Insitution" model etc.

I'm using Django's generic relations to allow each of these objects to create a foreign-key to Address.

However, this seems to cause some weirdness in the Django Admin (or perhaps I'm not understanding correctly how it's meant to be used). In the Django Admin, if I try to create an address, I'm seeing fields for "Content type" and "Object id". The model won't validate/save if these aren't filled. Not sure what to put in these.

The thing is, I wanted to be able to create standalone Address objects. Then, when I create a User Profile, or a Hospital, I could link these to the Address objects, including the possibility of multiple ones linking to the same Address object.

How should I use the Django admin with generic relations?

Also, I'm also intending to use django-reversion for version control of the models, not sure if this will cause any issues with generic relations and the admin?

Cheers, Victor

Edit: I should just add, here's an earlier question I posted Addresses and Inlines:

Django - Designing Model Relationships - Admin interface and Inline

Based on the answers given there, that's why the Address model is the one with a foreign key. And since a normal FK field can only point to one type of object, that's why we're using generic relations.

Each User/Department/Hospital etc. may (and in most cases will) have multiple addresses.

The same address can be used by multiple entities, but this is rarer, and duplication is fine here, I'm guessing, right?

So it'd be a one-to-many from User/Department/Hospital to Addresses.

In that original question, they also suggested using abstract classes, and a different Address model for each entity that needed an Address. I'm still not sure if that's the better approach, or if there's a way to make GenericRelations work with what I'm trying to do here.

解决方案

Using generic relations in django is exactly that. A ForeignKey to ContentType (content_type) and an IntegerField to denote the instance id (object_id). These are useful if you don't know which type of content the ForeignKey is pointing to. Since you know you're targeting the Address model you want to use regular ForeignKey(Address) instead of generic relations.

In response to your comment

Actually its much easier to use ForeignKey since you don't need to go through ContentType.

class Address(models.Model):
  street=models.CharField(max_length=100)
  city=models.CharField(max_length=100)


class Hospital(models.Model):
  name=models.CharField(max_length=100)
  created=models.DateTimeField()
  address=models.ForeignKey(Address, related_name="hospitals")

class Institution(models.Model):
  name=models.CharField(max_length=100)
  address=models.ForeignKey(Address, related_name="institutions")


>>> instance=Institution.objects.get(id=1)
>>> instance.address.city
>>> address=Address.objects.get(id=1)
>>> address.institutions.all() 
>>> address.hospitals.all()

Are your models going to share addresses? i.e. Can a Hospital and an Institution and perhaps a UserProfile all point to the same address instance? Or is it more likely that each will have it's own address? I am trying to understand why you've created a separate class Address. If it's to avoid retyping the same fields into each class you could use an abstract model class and subclass it. Or you might be needing a OneToOneField which is a two way pointer between the two instances.

这篇关于Django管理员Django通用关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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