Django General Relationship with Django Administrator

I have a Django project that has an Address model. This is used in several places - according to the User Profile model, according to the Hospital model, according to the Fastening model, etc.

I use Django's general relationship so that each of these objects can create a foreign key for the address.

However, this seems to cause some oddities in Django Admin (or maybe I don't understand how this should be used). In Django Admin, if I try to create an address, I see the fields for "Content Type" and "Object ID". The model will not check / save if they are not filled. Not sure what to add to them.

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 associate them with Address objects, including the possibility of multiple connections to the same Address object.

How do I use a Django admin with general relationships?

In addition, I also intend to use django-reversion to control model versions, not sure if this will cause any problems with the generic relationship and the administrator?

Cheers, Victor

Edit: I should just add, here is an earlier question that I posted Addresses and lines:

Django - Designing Model Relationships - Admin and Inline Interface

, , Address . FK , .

// .. ( ) .

, , , , ?

, -- // .

, . , , GenericRelations , .

+3
1

. A ForeignKey ContentType (content_type) IntegerField (object_id). , , ForeignKey. , Address, ForeignKey(Address) .

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

? .. Hospital Institution , , a UserProfile ? , , ? , Address. , . OneToOneField, .

+3

Source: https://habr.com/ru/post/1729016/


All Articles