Thursday, February 24, 2011

Re: Multi-table Inheritance: How to add child to parent model?

I had now a similar problem and started to write a generic solution
which is certainly not perfect, but works for me. Here is a detailed
description:

http://lino.saffre-rumma.net/autodoc/lino.test_apps.1.html

Looking forward to any comments.
Luc


On 7.11.2010 17:19, ringemup wrote:
> Thank you for the pointer to ticket #11618, Stefan. At this point, I
> don't care if the workaround is ugly -- if it actually works, at least
> I'll be able to move forward with this project. I'll test it out and
> check back in to confirm whether it does.
>
>
> On Nov 7, 9:03 am, Stefan Foulis <stefan.fou...@gmail.com> wrote:
>> As far as I know there is no existing api to convert a 'Place' into a
>> 'Restaurant' with multi-table inheritance in django. There is a ticket
>> about this [1]. And in another ticket [2] that is marked as a
>> duplicate but seems to have an example of how to solve this problem
>> until there is a standard api for the operation. Translated to the
>> Restaurant example this would be:
>>
>> restaurant = Restaurant(place_ptr = place)
>> for f in place._meta.local_fields: setattr(restaurant, f.name,
>> getattr(place, f.name))
>> restaurant.save()
>>
>> [1]http://code.djangoproject.com/ticket/7623
>> [2]http://code.djangoproject.com/ticket/11618
>>
>> On Nov 4, 9:25 pm, ringemup <ringe...@gmail.com> wrote:
>>
>>> I have an existing model that I want to extend using multi-table
>>> inheritance. I need to create a child instance for each parent
>>> instance in the database, but I can't figure out how. I've scoured
>>> google and haven't come up with anything other than Ticket #7623[1].
>>> Here are some of the things I've tried...
>>
>>> Let's adapt the Place / Restaurant example from the docs:
>>
>>> class Place(models.Model):
>>> name = models.CharField(max_length=50)
>>> address = models.CharField(max_length=80)
>>
>>> class Restaurant(Place):
>>> place = models.OneToOneField(Place, parent_link=True,
>>> related_name='restaurant')
>>> serves_hot_dogs = models.BooleanField()
>>> serves_pizza = models.BooleanField()
>>
>>> I want to do the following, in essence:
>>
>>> for place in Place.objects.all():
>>> restaurant = Restaurant(**{
>>> 'place': place,
>>> 'serves_hot_dogs': False,
>>> 'serves_pizza': True,
>>> })
>>> restaurant.save()
>>
>>> Of course, doing this tries to also create a new Place belonging to
>>> the new Restaurant, and throws an error because no values have been
>>> specified for the name and address fields. I've also tried:
>>
>>> for place in Place.objects.all():
>>> restaurant = Restaurant(**{
>>> 'serves_hot_dogs': False,
>>> 'serves_pizza': True,
>>> })
>>> place.restaurant = restaurant
>>> place.save()
>>
>>> This, however, doesn't create any records in the restaurant table.
>>
>>> Any suggestions?
>>
>>> [1]http://code.djangoproject.com/ticket/7623
>>
>>
>

--
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 django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate