On Oct 20, 2017 4:15 AM, "Jani Tiainen" <rede...@gmail.com> wrote:

Hi.

I've resolved such a case with two nullable fkeys and a discriminator field
to tell which one fkey is used.


Another option that is slightly safer is to override the save() method to
set the opposing FK to None every time the model is saved. Your
'discriminator' is then based on which field is populated. It helps avoid
situations where a cascade deletion of the foreign object leaves your model
with no FK associations, but the model is still marked with a
discriminator, and the lack of an FK value can incur a bug. It's one less
field to set with the same net result.

There are use cases for a discriminator field though (ie calling functions
dynamically, or a status that can't be determined by presence or lack of
data, etc.), so you may still have a good solution. I just prefer to
minimize the fields that I'm managing.

-James

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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%2BciUk95Kben-4bKUEwPSzQiBFqKWGSy%2BXJiUFTa_4SuPUqw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to