Models inheritance in django: how to change PK "<model_name>_ptr_id" on "id"?

899 views
Skip to first unread message

Artyom Chernetzov

unread,
Feb 7, 2012, 1:26:32 PM2/7/12
to django...@googlegroups.com
Here is model structure: Client is User, Client can be corporate or person:

    class Client(User):
        #fields
      
    class ClientCorporate(Client):
        #fields
        
        
    class ClientPerson(Client):
        #fields

And client can make orders:

    class Order(models.Model):
        client=models.ForeignKey(Client)
        #fields

I try to create common orders list for any type of client, here is view:

    def orders_list(request):
        client=request.user.client
        return list_detail.object_list(request,
            queryset = client.order_set.all(),
            #templates ...
            )

And it leads to an error:

**DatabaseError while rendering: no such column: cabinets_order.client_id**

I browse db and find that all User childs have column "user_prt_id". I can't find way to change pk name in inherited models. But maybe there is some better approach?

Andre Terra

unread,
Feb 7, 2012, 1:52:16 PM2/7/12
to django...@googlegroups.com
I just read your e-mail quick and somewhat carelessly, so forgive me if I'm missing something.


Here's a list of things for you to check:

* Have you defined your Client model with abstract = True in its Meta options?[1]
* Have you syncdb'd[2]?
* If you must name your pk something else, just follow the docs[3]

[1] http://django.me/abstract-base-classes
[2] http://django.me/syncdb
[3] http://django.me/automatic-primary-key-fields


Cheers,
AT



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/RrPQgVp3R2EJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

akaariai

unread,
Feb 7, 2012, 3:42:33 PM2/7/12
to Django users
Use OneToOneField with parent_link = True, then you can change the
name of the db field. See:
https://docs.djangoproject.com/en/dev/ref/models/fields/#onetoonefield

Artyom Chernetzov

unread,
Feb 8, 2012, 1:25:45 PM2/8/12
to django...@googlegroups.com
Thanks, everybody, problem was: I add ForeignKey field in object after dbsync. I just needed to reset database, it solve problem.

Jean-Mark

unread,
Feb 9, 2012, 8:09:17 AM2/9/12
to Django users
You can also use the south application to manage migrations.
pip install south

Then add 'south' to the list of installed apps.

To start off you can do
./manage.py schemamigration --initial appname
./manage.py migrate appname

Then whenever you make a change to your models:
./manage.py schemamigration --auto appname # this will show the
changes and ask some questions...
./manage.py migrate appname

JohnA

unread,
Feb 9, 2012, 2:34:25 PM2/9/12
to Django users
Glad you were able to resolve your problem. I use multi-table
inheritance a lot and so far it always "just works" without me having
to worry about or fiddle with the internal ids that Django uses to
connect the derived class rows to the base class ones.

-- John

On Feb 8, 1:25 pm, Artyom Chernetzov <achernet...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages