Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing unique together with model inheritance

I was trying to resolve the issue below, after some searching it seems to be an open bug in Django. I resolved the issue by adding a classmethod to the model child, although this solution works, it still requires another custom check on any (Model)Form using this child class. I'm posting this for others to find a solution sooner than I did, other solutions are welcome too.

class Foo(models.Model):
    attr1 = models.IntegerField()
    attr2 = models.IntegerField()

    class Meta:
        unique_together = (
            ('attr1', 'attr2'),
        )


class Bar(Foo):
    attr3 = models.IntegerField()

    class Meta:
        unique_together = (
            ('attr1', 'attr3'),
        )

raises:

Unhandled exception in thread started by <bound method Command.inner_run of <django.contrib.staticfiles.management.commands.runserver.Command object at 0x10f85a0d0>>
Traceback (most recent call last):
  File "/Users/intelliadmin/VirtualEnvs/virtenv9/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 91, in inner_run
    self.validate(display_num_errors=True)
  File "/Users/intelliadmin/VirtualEnvs/virtenv9/lib/python2.7/site-packages/django/core/management/base.py", line 270, in validate
    raise CommandError("One or more models did not validate:\n%s" % error_text)
django.core.management.base.CommandError: One or more models did not validate:
app.Bar: "unique_together" refers to attr1. This is not in the same model as the unique_together statement.
like image 304
Hedde van der Heide Avatar asked Mar 01 '13 15:03

Hedde van der Heide


1 Answers

A possible solution:

class Bar:
    # fields...

    @classmethod
    def _validate_unique(cls, self):
        try:
            obj = cls._default_manager.get(attr1=self.attr1, attr3=self.attr3)
            if not obj == self:
                raise IntegrityError('Duplicate')
        except cls.DoesNotExist:
            pass

    def clean(self):
        self._validate_unique(self)
        super(Bar, self).clean()
like image 171
2 revs Avatar answered Sep 20 '22 13:09

2 revs