Prevent permutations of foreign keys in Django

I have a django Model as follows:

class ModelName(models.Model): field_one = models.ForeignKey(Table_One, related_name="field_one") field_two = models.ForeignKey(Table_One, related_name="field_two")

I know unique_together can be used to make sure duplicate entries cant be made.

Eg, if for the above table:

unique_together = ['field_one', 'field_two']

Then you can enter values such as A for field_one and B for field_two. However, you cannot make the same entry again. But this also allows entry B for field_one and A for field_two, which according to my controller logic is the same as A and B.

I need to make sure that if A and B are entered for the respective fields, B and A cannot be entered again.

How do I allow only entries of unique combinations?


Sample clean validation code,

from django.db.models import Q def clean(self): mn = ModelName.objects.filter(Q(field_one=self.field_one,field_two=field_two)|Q(field_one=self.field_two,field_two=field_one)) if mn.exists(): raise ValidationError('Already exist')


I would override the model's clean method and raise a ValidationError. It's a place where you can enforce extra constraints on your models.

Note however that this method will only be called after a ModelForm's is_valid().

Validating objects


  • WCF Service Application returns 404 using WebGet
  • Determining if a string has been entered with a leading single quote
  • Type mismatch: cannot convert from Connection to Connection
  • Intellij Idea Terminal shortcut not working
  • Drag and Drop for imageview not working
  • Merge arrays by common column values in julia
  • Slowly changing dimension - What is Pure type 6 implementation
  • SQL append distinct values from two columns and two tables
  • Hibernate: Inheritance and relationship mapping + generics
  • Extracting individual digits from a float
  • Should I use composite primary keys in Grails?
  • Unix Network Programming Clarification
  • (Tcl/Expect) clear screen after exit
  • Scanner nextInt() and hasNextInt() problems
  • Django model for a Postgres view
  • jQuery: How to AJAXify WordPress Search?
  • Trying to get the char code of ENTER key
  • Reading a file into a multidimensional array
  • RxJava debounce by arbitrary value
  • Android application: how to use the camera and grab the image bytes?
  • Insert new calendar with SyncAdapter- Calendar API Android
  • onBackPressed() not being executed
  • Sencha Touch 2.0 Controller refs attribute not working?
  • How to clear text inside text field when radio button is select
  • Scrapy recursive link crawler
  • Convert array of 8 bytes to signed long in C++
  • ActionScript 2 vs ActionScript 3 performance
  • How do you troubleshoot character encoding problems?
  • Delete MySQLi record without showing the id in the URL
  • What do the 'size' numbers mean in the windbg !heap output?
  • Unanticipated behavior
  • bootstrap to use multiple ng-app
  • Comma separated Values
  • Trying to get generic when generic is not available
  • Understanding cpu registers
  • Why joiner is not used after Sequence generator or Update statergy
  • Recursive/Hierarchical Query Using Postgres
  • Running Map reduces the dimensions of the matrices
  • Binding checkboxes to object values in AngularJs
  • How to load view controller without button in storyboard?