开发者

Django Model - Inherited Fields (Sorta ?)

how can I structure the RegisteredSubscriber model to achieve the functionality described in the pseudo code below?

I guess I could have implemented a function for each field, but that doesn't seem correct and is kinda labourous. Thoughts?

 61 class RegisteredSubscriber(Subscriber):
 62     user = models.ForeignKey(User)
 63     first_name = self.user.first_name
 64     last_name = self.user.last_name
 65     email = self.user.email
 66
 67 class AnonymousSubscriber(Subscriber):
 68     first_name = models.CharField(max_length = 100)
 69     last_name = models.CharField(max_length = 100, blank = True)
 70     email = models.EmailField(unique = True)

Okay, this is achieves the functionality I wanted, but I'm sure开发者_JAVA技巧 its not proper. Thoughts? What would be the formal method, have I made a design error?

 59 class RegisteredSubscriber(Subscriber):
 60     user = models.ForeignKey(User, unique = True)
 61     first_name = 'candy'
 62     last_name = 'candy'
 63     email = 'candy'
 64
 65     def __init__(self, *args, **kwargs):
 66         super(RegisteredSubscriber, self).__init__(*args, **kwargs)
 67         
 68         if self.id:
 69             self.first_name = self.user.first_name
 70             self.last_name = self.user.last_name
 71             self.email = self.user.email
 72
 73     def __unicode__(self):
 74         return u'%s %s' % (self.first_name, self.last_name)


Since you already have a ForeignKey relationship to the User, I don't understand the need to have first_name, last_name, email fields in the RegisteredSubscriber model, unless i'm missing something. If you want to access the first_name etc fields using the RegisteredSubscriber instance the ForeignKey relation ship allows you to use __. eg: user__first_name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜