S
S
Shurik Skalkovich2015-07-10 20:20:39
Django
Shurik Skalkovich, 2015-07-10 20:20:39

How to reference id in model in new versions of django?

Just started learning Django.
For this piece of code, Django throws an error during migration.

class DescriptionLinks(models.Model):
    class Meta:
        db_table = "description_link"
    name = models.CharField(max_length = 100)
    description = models.TextField()

class SpecificLinks(models.Model):
    class Meta:
        db_table = "specific_link"
    link = models.SlugField(max_length = 200)
    description = models.ForeignKey(DescriptionLinks)

You are trying to add a non-nullable field 'project' to news without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows)
2) Quit, and let me add a default in models.py
Select an option:
Found a similar problem and solution that added a mandatory the description field in the SpecificLinks model, but the DescriptionLinks object already exists and django doesn't know what to put in the description field.
There is also a code example in the doc:
def contact_default():
    return {"email": "[email protected]"}

contact_info = JSONField("ContactInfo", default=contact_default)

The question is why this is done, because in older versions, django referred to the id for the foreign key. And how to refer to id in new versions?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Z
zigen, 2015-07-10
@OuroborosSK

Add the following to the project field: null=True, blank=True
Or after migrating, select 1, and then specify 1 too :)

A
Alexander, 2015-07-10
@syschel

class Meta:
        db_table = "....."

It's redundant in the models. Specifically db_table.

V
Vladislav Sklyar, 2015-07-10
@VladSkliar

You can press 1. You will be prompted to fill in an existing table with data
.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question