V
V
Vitalik logvinenko2014-04-28 11:29:07
PostgreSQL
Vitalik logvinenko, 2014-04-28 11:29:07

How to set unique pair of fields in django?

Tell me, please, is it possible in django, in the model settings, to set a unique pair of fields?
Analogy in sql:
CREATE TABLE Customers
( cnum integer NOT NULL,
cname char (10) NOT NULL,
city char (10),
rating integer,
snum integer NOT NULL,
UNIQUE (cnum, snum));

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2014-04-28
@Miren

Options.unique_together
Sets of field names that, taken together , must be unique:
unique_together = (("driver", "restaurant")),)
This is a tuple of tuples that must be unique when considered together. It's used in the Django admin and is enforced at the database level (ie, the appropriate UNIQUE statements are included in the CREATE TABLE statement).
For convenience, unique_together can be a single tuple when dealing with a single set of fields:
unique_together = ("driver", "restaurant")

class TTest(models.Model):
    t1 = models.IntegerField()
    t2 = models.IntegerField()

    class Meta:
        unique_together = ('t1', 't2')

$python manage.py sql ttest
BEGIN;
CREATE TABLE "ttest_ttest" (
    "id" integer NOT NULL PRIMARY KEY,
    "t1" integer NOT NULL,
    "t2" integer NOT NULL,
    UNIQUE ("t1", "t2")
)
;

COMMIT;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question