views:

123

answers:

2

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.

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

A: 

Hunch says you could do with declaring your Entry class as an abstract one unless you need an Entry as an actual object, too

...rest of Entry model here...
created = models.DateTimeField(auto_now_add=True) ## ONLY set date when created, not every save

class Meta:
   abstract = True

...methods for your model etc...
stevejalim
I decided to go with multi-table inheritance because I need to query mixed bag of objects in one list. Based on the documentation this should work: http://docs.djangoproject.com/en/1.1/topics/db/models/#id7
jorde
+2  A: 

The reason for this error appeared was because I used same column name that was already used with model name. Karen T. contributed the following answer in Django mailing list:

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.

jorde