Kwall
Kwall

Reputation: 549

How can we set a model is as default=0 or default=null in django

Two month back I had created two models and it was working well, and now these models have lot of query which is filled by users. It is clear that both models are independent to each other.

My previous models.py

class State(models.Model):
    state_name = models.CharField(max_length=300, blank=False)
    state_code = models.CharField(max_length=100, blank=False,unique=True)

    @classmethod
    def get_all_states(self):
        return State.objects.all()

    def __str__(self):
        return self.state_name

class College(models.Model):
    college_name = models.CharField(max_length=300, blank=False)
    college_code = models.CharField(max_length=100, blank=False,unique=True)
    university_name = models.CharField(max_length=300, blank=True)

    def __str__(self):
        return self.college_name

Today I want to design College model according to state wise. If users have selected some state than related college list will come. For this condition I have created below models.py

class State(models.Model):
    state_name = models.CharField(max_length=300, blank=False)
    state_code = models.CharField(max_length=100, blank=False,unique=True)

    @classmethod
    def get_all_states(self):
        return State.objects.all()

    def __str__(self):
        return self.state_name

class College(models.Model):
    college_name = models.CharField(max_length=300, blank=False)
    college_code = models.CharField(max_length=100, blank=False,unique=True)
    university_name = models.CharField(max_length=300, blank=True)
    state_id = models.ForeignKey(State, on_delete=models.CASCADE)

    @classmethod
    def get_all_colleges(self):
        return self.College.objects.all()

    def __str__(self):
        return self.college_name

In code you can see state_id (in College models) is a foreign key. When I did migration then It's showing the error

You are trying to add a non-nullable field 'state_id' to college without a default; we can't do that (the database needs something to populate existing rows).

I tried to put default=null but it's showing error like can't do it, I also tried with default=0 but it's showing migration error ('app', College).

state_id = models.ForeignKey(State, on_delete=models.CASCADE, default=0)
state_id = models.ForeignKey(State, on_delete=models.CASCADE, default=null)

  1. How can we fix it?
  2. Can we put default=null or default=0 in state_id objects?
  3. If not then alternatively What would be the solution?
  4. I got some solution on search but they are telling delete the table or cleared the College table data and then try to migrate again, but this will work when we are trying on localhost. What if we are trying on production server?

Upvotes: 0

Views: 1862

Answers (2)

qubitslover
qubitslover

Reputation: 17

Since you have data in the table you will need to add an attribute to the model field null=True and do the migrations. The field should look like,

state_id = models.ForeignKey(State, on_delete=models.CASCADE, null=True)

Doing this will no more show you add default error.

And in production what you can do is again leave it null and create the relation manually.

For eg: if you are creating a profile table to an existing user table you can run a condition checking if the user has a profile if not create one.

See the documentation: https://docs.djangoproject.com/en/3.2/ref/models/fields/

Upvotes: 0

AlexTorx
AlexTorx

Reputation: 775

You are having this error because you already have rows in your database and you are trying to create a new column that must not be empty without specifying a value that will be inserted in already existing rows. When building the migration file, django should ask you what value you would like to set for this field for already existing rows when running python manage.py makemigrations.

However if you prefer not to set a default value but allow the state_id field to be empty, you should set null=True in your foreign key definition.

Upvotes: 1

Related Questions