5

I want to ensure an object is unique, and to throw an error when a user tries to save it (e.g. via the admin) if not? By unique, I mean that some of the object's attributes might hold the same values as those of other objects, but they can't ALL be identical to another object's values.

If I'm not mistaken, I can do this like so:

class Animal(models.Model):
    common_name = models.CharField(max_length=150)
    latin_name = models.CharField(max_length=150)
    class Meta:
        unique_together = ("common_name", "latin_name")

But then each time I refactor the model (e.g. to add a new field, or to change the name of an existing field), I also have to edit the list of fields in the parenthesis assigned to unique_together. With a simple model, that's OK, but with a substantial one, it becomes a real hassle during refactoring.

How can I avoid having to repeat typing out the list of field names in the unique_together parenthesis? Is there some way to pass the list of the model's fields to a variable and to assign that variable to unique_together instead?

3
  • 1
    +1 - great question. I don't think you can, but I'm interested to hear others' opinions. Sep 7, 2009 at 13:33
  • +1 - It really is interesting if someone can find a nice solution. I tried but could not make it work via reflection, because you cannot read the properties from class Animal (which is obvious because Animal is not completely defined at that moment).
    – FlorianH
    Sep 7, 2009 at 13:47
  • jtiai on irc://irc.freenode.net/django kindly took some time to discuss the question and suggested I could 'add my own "AllUniqueModel" class, inherit standard "Model" and make some metaclass magic to inject all fields in unique_togerther after they're processed by standard creation procedure.' I'm not entirely sure what jtiai meant by that, but it sounds like a more complicated solution than I was hoping for.
    – user82216
    Sep 7, 2009 at 14:38

1 Answer 1

4

Refactoring models is a rather expensive thing to do:

  • You will need to change all code using your models since field names correspond to object properties
  • You will have to change your database manually since Django cannot do this for you (at least the version I used the last time when I worked with Django couldn't)

Therefore I think updating the list of unique field names in the model meta class is the least issue you should worry about.

EDIT: If you really want to do this and all of your fields must be "unique together", then the guy at freenode is right and you'll have to write a custom metaclass. This is quite complicated and errorprone, plus it might render your code incompatible to future releases of Django.

Django's ORM "magic" is controlled by the metaclass ModelBase (django.db.models.base.ModelBase) of the generic base class Model. This class is responsible to take your class definition with all fields and Meta information and construct the class you will be using in your code later.

Here is a recipe on how you could achieve your goal:

  1. Subclass ModelBase to use your own metaclass.
  2. Override the method __new__(cls, name, bases, dict)
  3. Inspect dict to gather the Meta member (dict["Meta"]) as well as all field members
  4. Set meta.unique_together based on the names of the fields you gathered.
  5. Call the super implementation (ModelBase.__new__)
  6. Use the custom metaclass for all your unique models using the magic member __metaclass__ = MyMetaclass (or derive an abstract base class extending Model and overriding the metaclass)
3
  • 1
    At the prototyping stage, refactoring models is cheap but duplicating code is expensive, hence my question. Obviously, in production, it's a different matter, but if a good answer to my question can be found, then this will at least reduce one of the steps I'd otherwise have to take when refactoring in production.
    – user82216
    Sep 7, 2009 at 14:54
  • 1
    See my updated response for the long answer, maybe you will understand that it might be too complicated to do. Sep 7, 2009 at 15:10
  • 2
    Thank you for updating your response. Yes, I see it's quite a hassle to achieve this. Hmm. It would be so nice if Django could simply provide the option to specify unique_together = (all) or somesuch, or if it could allow models to perform introspection on themselves to generate field lists for passing to Meta options, etc. Or both :)
    – user82216
    Sep 7, 2009 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.