On 19 fév, 18:00, ALJ <astley.lejas...@gmail.com> wrote: > I want to set a value in a parent model based on the child class.
childs inherit parents attributes, so you don't have to "set a value" (I assume you meant : "set an attribute's value") "in the parent model" - just set it in the child. > So, > in this case, I would set the ExtendedUser.usertype depending on > whether its "Teacher" or "Student". You define usertype as a foreign key on a UserType model (which seems rather weird to me but let's ignore this for now), but you didn't provide the definition of this model class. > I tried looking at using '%(class)s' to get the class name, but that > didn't work. ??? The class of a Python object f is accessible as f.__class__ > Or can you explicitly pass values from the child classes? "pass" to what ? > >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>. > > def set_user_type(): > '''somehow find and return the user type based on the child > class''' > > class ExtendedUser(models.Model): > user = models.OneToOneField(User, blank=True, related_name='% > (class)s') > usertype = models.ForeignKey(UserType, default=set_user_type()) I think you really should take a couple hours learning Python - this would save you a lot of time. Sorry but trying to explain why this won't do what you think - and why this just can't do what you want - would require rewriting too much of the FineManual(tm). Anyway... if you know how to get the correct UserType for a given class object, the following code should get you started: def get_usertype_for(cls): # code that returns the correct UserType value # based on the class # NB : 'cls' is the class *object*. # if you want the class name, it's accessible as # cls.__name__ raise NotImplementedError("TODO") class ExtendedUser(models.Model): user = models.OneToOneField( User, blank=True, related_name='%(class)s' ) usertype = models.ForeignKey( UserType ) def save(self, *args, **kw): if not self.pk: self.usertype = get_user_type_for(self.__class__) super(ExtenderUser, self).save(*args, **kw) And that's all folks... Oh, and please do yourself a favour : learn Python. HTH -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.