class MyChoices(models.IntegerChoices): CHOICE_A = 1 CHOICE_B = 2 class MyModelA(models.Model): choice = models.IntegerField(choices=MyChoices.choices) payload = models.CharField(...) class MyModelB(models.Model): models_a = models.ManyToManyRelationshipField(MyModelA, ...) payload = models.CharField(...)
Is there a way I can create a
UniqueConstraint
onapp_my_model_b_my_model_a
table withMyModelA.choice
field andMyModelB
primary key field?
score:0
You can define a model as through=…
model [Django-doc]:
class MyChoices(models.IntegerChoices):
CHOICE_A = 1
CHOICE_B = 2
class MyModelA(models.Model):
choice = models.IntegerField(choices=MyChoices.choices)
class MyModelB(models.Model):
models_a = models.ManyToManyRelationshipField(MyModelA, through='MyModelC')
class MyModelC(models.Model):
model_a = models.ForeignKey(MyModelA, on_delete=models.CASCADE)
model_b = models.ForeignKey(MyModelB, on_delete=models.CASCADE)
class Meta:
constraints = [
models.UniqueConstraint(fields=['model_a', 'model_b'], name='unique_a_b')
]
Credit To: stackoverflow.com
Related Query
- How to create an UniqueConstraint for M2M relationship table in Django ORM?
- How do I tell Django to not create a table for an M2M related field?
- How do I write a Django ORM query for the reverse relationship in a one-to-many relationship?
- How to use base class in models.py but NOT have django create a table for the base class?
- How to order by a field from the `through` table for a M2M relationship in Django?
- How can make django don't create id column for database table
- Django Customer with onetoone relationship with User - how to create the fields for that model when creating User?
- How to create an object for a Django model with a many to many field?
- In a Django QuerySet, how to filter for "not exists" in a many-to-one relationship
- How to create table during Django tests with managed = False
- How to query directly the table created by Django for a ManyToMany relation?
- How can I create custom page for django admin?
- How to remove the prefix of a table for a Django model?
- How to create an alias for Django Model field?
- how to create serializer for an enum field in django rest framework
- How do I create a new database for an existing App using Django South and set default values?
- How to create a triple-join table with Django
- How to programmatically generate the CREATE TABLE SQL statement for a given model in Django?
- Django Rest Framework - how to create custom error messages for all ModelSerializer fields?
- How to create a temporary table and not lose the ORM in django?
- Using the Django ORM, How can you create a unique hash for all possible combinations
- How to create factory-boy factories for Django models with the same foreign key
- How to fake migrations for not to create a specific existing intermediary table
- how to create a Django models that doesn't map to a database table
- Django admin site: how to create a single page for global settings?
- How to create mock LDAP server for Django project?
- How to maintain insert ordering for one-to-many relationship in Django
- How to create index for django sitemaps for over 50.000 urls
- How to automatically create postgis database for Django testing?
- How to create a one time link on Django for video file
More Query from same tag
- Why can't I view pages in the admin section of django-cms?
- Converting timezone-aware datetime to local time in Python
- Django Queryset filter for arguments in a list
- Refactor long try-except chain
- how can restrict access to media image URL in django
- On button click add data from model to a new form
- Django - Cache vs QuerySet Filter for Server Efficiency?
- Django development server keeps logging out
- How to sort translated modelform fields alphabetically
- AngularJS + Django Rest Framework + CORS ( CSRF Cookie not showing up in client )
- 2 Bitnami Django Projects Running at same time Apache WSGI
- django-hstore DictionaryField returns string rather than a dictionary
- Django rest framework Api documentation Swagger 2.0
- Django Test Runner Not Finding Test Methods
- Admin.sites.url password transmission