Django: "Fake" multiple inheritance

28 views
Skip to first unread message

mic...@herrmann.io

unread,
Aug 14, 2015, 10:35:26 AM8/14/15
to Django users
Hi all,

suppose I have the following model structure

    from django.db import models
    
    class Place(models.Model):
        name = models.CharField(max_length=50)
    
    class Restaurant(Place):
        ...
    
    class Hotel(Place):
        ...

I already have a Restaurant in my database. This Restaurant is now also becoming a Hotel. I would like to declare this in the database as follows:

    restaurant = Restaurant.objects.get(name='Will soon be a hotel')
    Hotel.objects.create(pk=restaurant.pk)

Is this a safe thing to do?

Thanks,
Michael

Daniel H

unread,
Aug 14, 2015, 5:17:03 PM8/14/15
to Django users
Hi Michael.

First of all, setting the pk to the pk of a different model will do nothing.

You can do this however, using Foreign Keys

restaurant = models.ForeignKey('Restautant')

Then declare a new hotel object like this:

restaurant = Restaurant.objects.get(name='Will soon be a hotel')
Hotel.objects.create(restaurant=restaurant.pk)

What does the "Place" table represent?

Michael Herrmann

unread,
Aug 16, 2015, 9:44:47 AM8/16/15
to Django users
Thank you Daniel,

I found another way.

Best,
Michael

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/26Qu9-wJ1gA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c270d98c-7a14-4870-9f04-3ccfcb3abeb3%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Inline image 1

Michael Herrmann, MSc
Alser Straße 18/26
1090 Wien
Tel.: +43 699 11 65 16 40

gst

unread,
Aug 16, 2015, 1:45:45 PM8/16/15
to Django users
Will you care share it?
I found the question interresting..
Thanks.

Michael Herrmann

unread,
Aug 17, 2015, 2:24:18 PM8/17/15
to Django users
Hi gst,

Instead of

    class Hotel(Place):
        ...

I ended up with

    class Hotel(object):
        place = models.OneToOneField(Place, primary_key=True)

I used a OneToOneField instead of ForeignKey because there can only be one hotel in a Place.

M

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/26Qu9-wJ1gA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.

James Schneider

unread,
Aug 17, 2015, 4:24:14 PM8/17/15
to django...@googlegroups.com

Hi gst,

Instead of

    class Hotel(Place):
        ...

I ended up with

    class Hotel(object):
        place = models.OneToOneField(Place, primary_key=True)

I used a OneToOneField instead of ForeignKey because there can only be one hotel in a Place.



Just to verify, you're not actually inheriting from (object), right? You're using Hotel(models.Model), I hope?

-James 

Michael Herrmann

unread,
Aug 17, 2015, 4:25:47 PM8/17/15
to Django users
Yes, of course you're right James, sorry. My classes are actually named differently and I was just trying to get the idea across.
M

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/26Qu9-wJ1gA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.

James Schneider

unread,
Aug 17, 2015, 7:14:18 PM8/17/15
to django...@googlegroups.com

Just making sure. Good luck!

-James

You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages