3

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.

4

1 に答える 1

3

django で一般的な関係を使用することはまさにそれです。ForeignKeyContentType (content_type) への AIntegerFieldと、インスタンス ID (object_id) を示すための 。ForeignKeyこれらは、が指しているコンテンツの種類がわからない場合に役立ちます。モデルをターゲットにしていることがわかっているので、一般的なリレーションではなくAddress通常のリレーションを使用しますForeignKey(Address)

あなたのコメントに応えて

ForeignKeyContentType を使用する必要がないため、実際にははるかに簡単に使用できます。

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()

モデルはアドレスを共有しますか? つまり、aHospitalと anInstitutionと、おそらく a のUserProfileすべてが同じアドレス インスタンスを指すことはありますか? それとも、それぞれが独自のアドレスを持つ可能性が高いですか? 別のクラスを作成した理由を理解しようとしていますAddress。同じフィールドを各クラスに再入力することを避ける場合は、抽象モデル クラスを使用してサブクラス化できます。OneToOneFieldまたは、2 つのインスタンス間の双方向ポインターであるが必要な場合があります。

于 2010-01-19T04:59:25.497 に答える