views:

332

answers:

1

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?

+2  A: 

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)
Ferdinand Beyer
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.
sampablokuper
See my updated response for the long answer, maybe you will understand that it might be too complicated to do.
Ferdinand Beyer
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 :)
sampablokuper