--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/043cc4db-f1a9-3beb-cf44-f63c401114ac%40djangodeployment.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f1fcc861-b3f3-43dc-9bad-a2511de4da44%40googlegroups.com.
In my project, I have a contacts model.
I have separate models, such as Person, Institution, Supplier, or Shipper that all have a foreign key to contacts.
So it looks like you’re doing it the way that I would do it.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/72D4C974-6300-4F06-8C80-C4E0EA9D398F%40gmail.com.
Hi.I've resolved such a case with two nullable fkeys and a discriminator field to tell which one fkey is used.
What I do is I create a superclass that I call Lentity (short for "legal
entity", despite the fact that it could refer to a group of people and is not
necessary legal) and the two subclasses Person and Organization, with
multi-table inheritance.
Hello James,
You are right that the correct term in everyday language for the
superclass of organization and person is "entity". However, I
didn't want to name it "entity" in the code, because, really,
"entity" is a different thing in programming, it's a term like
"object". It would be very confusing. The reason I didn't name it
LegalEntity is exactly that it might not be legal. I chose to
invent a new term, "Lentity", which ensures that someone will not
(mis)understand it just by looking at the name and will
necessarily look it up.
I think that my design is "correct" from a theoretical point of view. Inheritance is the way to solve such a problem in OOP, and multi-table inheritance is the way to solve this problem in relational databases. That's what the books say anyway.
In theory, theory and practice are the same. In practice they
differ. :-)
My design generally works and I'm quite satisfied with it. This doesn't mean it's without problems. So, for example, the original definition of Lentity in an app of mine was this:
class Lentity(models.Model): def __str__(self): try: return str(self.person) except Person.DoesNotExist: return str(self.organization) class Meta: verbose_name_plural = 'Lentities'
Then, when I had too many persons and organizations, I had to use raw_id_fields in the admin, and for this I had to make this change:
class Lentity(models.Model): # descr is a redundant field; see save() for more information. descr = models.CharField(max_length=111, blank=True, editable=False) def __str__(self): try: return str(self.person) except Person.DoesNotExist: return str(self.organization) def save(self, *args, **kwargs): # descr is a redundant, calculated field that is automatically filled # in during save(). We introduce this redundancy because there is # currently no other way in which the Django admin can sort lentities, # i.e. we can use descr as a field in "ordering" or "get_ordering", but # it's not possible to use an expression. (We need the Django admin to # manage lentities in general, as well as persons and organizations, in # order to make it possible to use raw_id_fields in AuthorInline.) self.descr = str(self) super(Lentity, self).save(*args, **kwargs) class Meta: verbose_name_plural = 'Lentities'So I had to put Lentity in the admin, ensuring users have no permissions to edit or delete or create "Lentity" directly, and I told users to just ignore that. (I'm can't tell whether these problems are worse than having two fields plus a discriminator field in the admin).
Antonis Christofides http://djangodeployment.com
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciX21jrrdceNoq4VpthKoc%2BH-g2LbOs%3D4xkV5HKxo0cVJA%40mail.gmail.com.
Hello James,
You are right that the correct term in everyday language for the superclass of organization and person is "entity". However, I didn't want to name it "entity" in the code, because, really, "entity" is a different thing in programming, it's a term like "object". It would be very confusing. The reason I didn't name it LegalEntity is exactly that it might not be legal. I chose to invent a new term, "Lentity", which ensures that someone will not (mis)understand it just by looking at the name and will necessarily look it up.
I think that my design is "correct" from a theoretical point of view. Inheritance is the way to solve such a problem in OOP, and multi-table inheritance is the way to solve this problem in relational databases. That's what the books say anyway.
In theory, theory and practice are the same. In practice they differ. :-)
My design generally works and I'm quite satisfied with it. This doesn't mean it's without problems. So, for example, the original definition of Lentity in an app of mine was this:
class Lentity(models.Model): def __str__(self): try: return str(self.person) except Person.DoesNotExist: return str(self.organization)
class Meta: verbose_name_plural = 'Lentities'Then, when I had too many persons and organizations, I had to use raw_id_fields in the admin, and for this I had to make this change:
class Lentity(models.Model): # descr is a redundant field; see save() for more information. descr = models.CharField(max_length=111, blank=True, editable=False) def __str__(self): try: return str(self.person) except Person.DoesNotExist: return str(self.organization) def save(self, *args, **kwargs): # descr is a redundant, calculated field that is automatically filled # in during save(). We introduce this redundancy because there is # currently no other way in which the Django admin can sort lentities, # i.e. we can use descr as a field in "ordering" or "get_ordering", but # it's not possible to use an expression. (We need the Django admin to # manage lentities in general, as well as persons and organizations, in # order to make it possible to use raw_id_fields in AuthorInline.) self.descr = str(self) super(Lentity, self).save(*args, **kwargs) class Meta: verbose_name_plural = 'Lentities'So I had to put Lentity in the admin, ensuring users have no permissions to edit or delete or create "Lentity" directly, and I told users to just ignore that. (I'm can't tell whether these problems are worse than having two fields plus a discriminator field in the admin).