You are right, and my sample function above "extend" also works. The problem is that I had a read-only property in my child model that had the same name as one of the fields in the parent model. I had not noticed that until I dug deeper to investigate the issue :-(
thanks for your help. -- Medhat On May 1, 11:49 am, AmanKow <[EMAIL PROTECTED]> wrote: > I did the following, it works fine: > > from django.db import models > > class Place(models.Model): > name = models.CharField(max_length=50) > address = models.CharField(max_length=80) > > def __unicode__(self): > return self.name > > # you can call this on any model object > def object_as_dict(model_object): > return > model_object.__class__.objects.filter(pk=model_object.pk).values()[0] > > # If you would like to add this ability to your model objects > # you could define this as a method in Place, > # but let's assume that you don't have access to Place > Place.as_dict = object_as_dict > > class Restaurant(Place): > serves_hot_dogs = models.BooleanField() > serves_pizza = models.BooleanField() > > class Bar(Place): > has_happy_hour = models.BooleanField() > checks_id = models.BooleanField() > > # now you can do like the following: > # >>> from mysite.eatery.models import Place, Restaurant, Bar, > object_as_dict > # >>> p = Place(name="Lee's Tavern", address="Staten Island") > # >>> p.save() # now we have a pre-existing record, IRL you would > fetch it > # >>> r = Restaurant(serves_pizza=True, serves_hot_dogs=False, > **p.as_dict()) > # >>> r.save() > > On Apr 30, 1:48 pm, medhat <[EMAIL PROTECTED]> wrote: > > > Trying to use this idea, I created the following function: > > > def extend(parent_class, parent_id, child_class, **kwargs): > > p = parent_class.objects.filter(pk=parent_id).values()[0] > > p.update(kwargs) > > c = child_class(**p) > > return c > > > but that still does not work. It throws an "AttributeError: can't set > > attribute" from line 224 in django\db\models\base.py > > > -- > > Medhat --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---