Django, I have a question.

Asked 2 years ago, Updated 2 years ago, 47 views

I have a question. You are trying to add a non-nullable field 'address' to register without a default; we can't do that (the database needs something to popul ate existing rows). Please select a fix:

That's the message. models.py

class Register(models.Model):
    user=models.OneToOneField(User,on_delete=models.CASCADE)
    company=models.CharField(max_length=30)
    office=models.CharField(max_length=30)
    address=models.CharField(max_length=100)
    tel=models.CharField(max_length=30)

froms.py

class RegistrationForm(UserCreationForm):
    address=forms.CharField(widget=forms.TextInput (attrs=dict (required=True, max_length=100), label=_("address"))
    tel=forms.CharField(widget=forms.TextInput (attrs=dict (required=True, max_length=30), label=_("Contact"))

There was a name in Register models a few days ago, but when I changed it to address and added tel, What should I do when it pops up like that?

django

2022-09-22 08:56

1 Answers

You are trying to add a non-nullable field 'address' to register without a default; we can't do that (the database needs something to popul ate existing rows). Please select a fix:

This error occurs frequently when you modify the DB. This case occurs because when you modify the address field, you have not decided how to modify the existing information.

For example, there was no address field in the original Register model, but when you added a new address field to migrate, What information should be added to the address field in the register information (existing row)? That's what I'm asking.

There are two solutions.

The first sets the default value in the field. For example,

CharField (default = "Changshin-dong, Jongno-gu")

If you set it up like this and migrate, all the existing Register objects are set to Changshin-dong, Jongno-gu.

The second allows null.

CharField(null=True)

After this setting, the existing Register objects address becomes null when you migrate.


2022-09-22 08:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.