Model validation fails, when inherited model redeclare parent field

26 views
Skip to first unread message

shiva

unread,
Apr 3, 2012, 11:34:50 PM4/3/12
to Django users
Hello!

In our apps (that use Django 1.2.7) we use following models:

class BaseRegistration(models.Model):
exhibition = models.ForeignKey(...)
year = models.IntegerField(...)
user = models.ForeignKey('auth.User')

class BarcodeRegistration(BaseRegistration):
class Meta:
abstract = True

class EventRegistration(BarcodeRegistration):
event = models.ForeignKey(...)

I append *validate_unique* by *user* and *event* fields in
EventRegistration class:

class EventRegistration(BarcodeRegistration):
event = models.ForeignKey(...)
user = models.ForeignKey('auth.User')

class Meta:
unique_together = ('event', 'user')

But in django command manage.py validate fails with error:

exhibition.eventregistration: "unique_together" refers to user. This
is not in the same model as the unique_together statement.

After some "pdb'ing" of django.core.management.validation,
django.db.models.base, and django.db.models.options I found, that is
happend because:

In [13]: EventRegistration._meta.get_field('user') in
EventRegistration._meta.parents.keys()[0]._meta.local_fields
Out[13]: True

In [14]: EventRegistration._meta.get_field('user') in
EventRegistration._meta.local_fields
Out[14]: False

Is it's something wrong with our code?

Thanks

--
Shavrin Ivan

Jani Tiainen

unread,
Apr 4, 2012, 1:54:47 AM4/4/12
to django...@googlegroups.com
Hi,

You'ew hitting limitation of Django ORM which prohibits overriding fields.

https://docs.djangoproject.com/en/1.3/topics/db/models/#field-name-hiding-is-not-permitted

shiva

unread,
Apr 4, 2012, 2:17:48 AM4/4/12
to django...@googlegroups.com
Jani, thank you.

2012/4/4 Jani Tiainen <red...@gmail.com>:

> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> 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.
>

Reply all
Reply to author
Forward
0 new messages