0

Say I have a model like this:

class Book(models.Model):

    user = models.ForeignKey(User)
    book_isbn = models.CharField()

I would like the combination of the book and user fields to be unique. That is different users could have the same book, but a specific user could only have the book once in their list of books.

I thought unique_together would be the perfect solution, but them read it can't be used if any of the fields have a FK. What's the best way to achieve what I want?

011235813
  • 3
  • 1
  • It seems that this used to be a bug in Django a while back but has since been fixed (See: https://code.djangoproject.com/ticket/1751). Did you verify it? – Manoj Govindan Aug 18 '11 at 12:30
  • This was answered here: http://stackoverflow.com/questions/2201598/django-how-to-define-two-fields-unique-as-couple More on Django unique_together [here](https://docs.djangoproject.com/en/1.1/ref/models/options/#unique-together "djangoproject.com"). – Eric H. Feb 02 '12 at 19:32

1 Answers1

1

I'm currently using multiple django models with unique_together clause which contained foreign keys and I never had any problems. I currently using django 1.2.

Exelian
  • 5,749
  • 1
  • 30
  • 49