Thursday, 23 November 2023

django unique two columns, make migration and run

 https://stackoverflow.com/questions/2201598/how-to-define-two-fields-unique-as-couple

class MyModel(models.Model):
  field1 = models.CharField(max_length=50)
  field2 = models.CharField(max_length=50)

  class Meta:
    unique_together = ('field1', 'field2',)


https://realpython.com/django-migrations-a-primer/
python manage.py makemigrations historical_data

python manage.py migrate
Migrations for 'historical_data':
  historical_data/migrations/0001_initial.py
    - Create model PriceHistory

This creates the migrations file that instructs Django on how to create the database tables for the models defined in your application. Let’s have another look at the directory tree:

bitcoin_tracker/
|
├── bitcoin_tracker/
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
|
├── historical_data/
│   ├── migrations/
│   │   ├── 0001_initial.py
│   │   └── __init__.py

No comments:

Post a Comment