ValueError with multi-table inheritance in Django Admin

137 views
Skip to first unread message

Jori

unread,
May 16, 2010, 4:58:41 AM5/16/10
to Django users
Hi,

I have a problem when using MIT in my models. I created two new
classes which inherit model Entry:

class Entry(models.Model):
LANGUAGE_CHOICES = settings.LANGUAGES

language = models.CharField(max_length=2,
verbose_name=_('Comment language'), choices=LANGUAGE_CHOICES)
user = models.ForeignKey(User)
country = models.ForeignKey(Country, null=True, blank=True)

created = models.DateTimeField(auto_now=True)

class Comment(Entry):
comment = models.CharField(max_length=2000, blank=True,
verbose_name=_('Comment in English'))

class Discount(Entry):
discount = models.CharField(max_length=2000, blank=True,
verbose_name=_('Comment in English'))
coupon = models.CharField(max_length=2000, blank=True,
verbose_name=_('Coupon code if needed'))

After adding these new models to admin via admin.site.register I'm
getting ValueError when trying to create a comment or a discount via
admin. Adding entries works fine thou.

Error msg:

ValueError at /admin/reviews/discount/add/
Cannot assign "''": "Discount.discount" must be a "Discount" instance.
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/reviews/discount/add/
Exception Type: ValueError
Exception Value:
Cannot assign "''": "Discount.discount" must be a "Discount" instance.
Exception Location: /Library/Python/2.6/site-packages/django/db/models/
fields/related.py in __set__, line 211
Python Executable: /usr/bin/python
Python Version: 2.6.1

I'm running Django 1.1 and South 0.7 with this. To me this seems like
a textbook example made using Django docs but I can't seem to get it
working.

-Jori

Ps. I tried to find a solution to this via StackOverflow but with no
luck. I'll post the solution there is someone knows what's wrong with
this.

--
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.

Karen Tracey

unread,
May 16, 2010, 7:26:17 AM5/16/10
to django...@googlegroups.com
On Sun, May 16, 2010 at 4:58 AM, Jori <jtl...@gmail.com> wrote:
Hi,

I have a problem when using MIT in my models. I created two new
classes which inherit model Entry:

   class Entry(models.Model):
       LANGUAGE_CHOICES = settings.LANGUAGES

       language = models.CharField(max_length=2,
verbose_name=_('Comment language'), choices=LANGUAGE_CHOICES)
       user = models.ForeignKey(User)
       country = models.ForeignKey(Country, null=True, blank=True)

       created = models.DateTimeField(auto_now=True)

   class Comment(Entry):
       comment = models.CharField(max_length=2000, blank=True,
verbose_name=_('Comment in English'))
[remainder snipped]
 
The problem seems to be that you have named a field in your Comment model with the same name, only lower case. Comment inherits from Entry, using multi-table-inheritance. This adds a OneToOneField in Comment back to Entry, which has a side-effect of adding a 'comment' attribute to Entry. This is the attribute that lets you access the Comment associated with the Entry as a result of the OneToOneField in Comment, and by default it is given the name of the related model, all-lowercase.

The problem then occurs when the Comment model "inherits' all the fields/attributes of Entry: the inherited 'comment' attribute from Entry is apparently over-riding the specified comment field. That's probably a bug, but it appears to have been there since 1.0. I have not done any research to see if it's been reported.

As a workaround you can name your fields something other than the model name all lowercased, or you can explicitly specify the OneToOneField in the child models, specifying parent_link=True and something other than the model name all lowercased for related_name.

Karen
--
http://tracey.org/kmt/

Jori

unread,
May 16, 2010, 8:19:56 AM5/16/10
to Django users
Thanks a lot! It didn't cross my mind to check the "hidden" relations
as this was the first time for me with multi-table inheritance.

-Jori

On May 16, 2:26 pm, Karen Tracey <kmtra...@gmail.com> wrote:
> On Sun, May 16, 2010 at 4:58 AM, Jori <jtla...@gmail.com> wrote:
>
> The problem seems to be that you have named a field in your Comment model
> with the same name, only lower case. Comment inherits from Entry, using
> multi-table-inheritance. This adds a OneToOneField in Comment back to Entry,
> which has a side-effect of adding a 'comment' attribute to Entry. This is
> the attribute that lets you access the Comment associated with the Entry as
> a result of the OneToOneField in Comment, and by default it is given the
> name of the related model, all-lowercase.
>
> The problem then occurs when the Comment model "inherits' all the
> fields/attributes of Entry: the inherited 'comment' attribute from Entry is
> apparently over-riding the specified comment field. That's probably a bug,
> but it appears to have been there since 1.0. I have not done any research to
> see if it's been reported.
>
> As a workaround you can name your fields something other than the model name
> all lowercased, or you can explicitly specify the OneToOneField in the child
> models, specifying parent_link=True and something other than the model name
> all lowercased for related_name.
>
> Karen
> --http://tracey.org/kmt/
Reply all
Reply to author
Forward
0 new messages