Django Multi-Table Inheritance VS Specifying Explicit OneToOne Relationship in Models

后端 未结 5 2064
萌比男神i
萌比男神i 2021-01-30 11:53

Hope all this makes sense :) I\'ll clarify via comments if necessary. Also, I am experimenting using bold text in this question, and will edit it out if I (or you) find it distr

5条回答
  •  迷失自我
    2021-01-30 12:29

    I recently switched over to using models that inherit off of contrib.auto.models.User. My general observation is that in theory they're great, but sometimes they don't get auto-magically handled like they're supposed to.

    I think your decision regarding inheritance vs. OneToOne comes down to this:

    • Do I want to have Django automatically do something right 95% of the time, and need to debug that other 5%

    -OR-

    • Do I want to do something manually myself 100% of the time

    If you haven't seen it, the Scott Barham blog has a great post about inheriting off of User, and also building a custom back end to make sure that your custom object is being returned -- Extending the Django User.

    Additionally of interest would be the AutoOneToOne field provided by django-annoying. It's sort of a hybrid of the two approaches here -- there's no inheritance taking place, but Django is taking care of creating the matching OneToOneField if it's not present.

    Also, thornomad does make a good point about the redundancy in your models. You could easily implement an abstract class to clean that up as so (assuming you're doing manual OneToOne):

    class BaseExtendedUser(models.Model):
        user = models.OneToOneField(User, blank=True, related_name='profile')
        landline = models.CharField(blank=True, max_length=20)    
        mobile = models.CharField(blank=True, max_length=20)
        created_by = models.ForeignKey(User, editable=False, blank=True, related_name="created_users")    
        modified_by = models.ForeignKey(User, editable=False, blank=True, related_name="modified_users")
    
        class Meta:
            abstract = True
    
    class Administrator(BaseExtendedUser):
        province = models.ForeignKey(Province)
    
    class Principal(BaseExtendedUser):
        branchoffice = models.ForeignKey(BranchOffice)
    

提交回复
热议问题