Opened 8 years ago

Closed 7 years ago

Last modified 7 years ago

#14943 closed (invalid)

Meta attributes for Model refused to work with Mixin.

Reported by: xuqingkuang Owned by: nobody
Component: Database layer (models, ORM) Version: 1.2
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


Hi, I defined a mixin for global usage but it looks it's refused to working with unique_together meta attribute.

Code is following:

from django.db import models

class CommonModelMixin(object):
    name = models.CharField(max_length=1024)
    description = models.TextField()
    creator = models.ForeignKey('auth.User', blank=True, null=True, default=None, editable=False)
    creation_date = models.DateTimeField(auto_now_add=True)

class Data(CommonModelMixin, models.Model):
    product = models.CharField(max_length=1024)
    class Meta:
        unique_together = ('product', 'name')

class Order(CommonModelMixin, models.Model):
    is_active = models.BooleanField(default=True)
    class Meta:
        ordering = ['is_active', 'name']

But when syncdb it just report:

$ ./ syncdb
Error: One or more models did not validate: "unique_together" refers to name, a field that doesn't exist. Check your syntax.
app.order: "ordering" refers to "name", a field that doesn't exist.

I won't to inherit the mixin class from models.Model, because I have other mixins need to define, but it breaks the logical.

Any idea about it ?

Change History (2)

comment:1 Changed 7 years ago by Russell Keith-Magee

Resolution: invalid
Status: newclosed

Your mixin class isn't a Django model, so the metaclass handling doesn't get activated. You need to make your mixin an abstract base model.

Also - Trac shouldn't be used for 'What's wrong" queries. It's a place to log things that are clearly bugs in Django. If you're not sure if your problem is a bug in Django, or a bug in your own code, you should ask on django-users first.

comment:2 Changed 7 years ago by Jacob

milestone: 1.3

Milestone 1.3 deleted

Note: See TracTickets for help on using tickets.
Back to Top