How to change Django model with content in the database - python

I have a model with a CharField that has a choices set, but I need to change the field to a ForeignKey field. I have made the changes but, when I make the migration, I got an error saying django.db.utils.DataError: invalid input syntax for integer: "GRAND_TOURISM", that I realize means that the current content of the field doesn't match the new foreign reference constraint.
Is there any way I can instruct Django to change current values to Null when migrating?

Related

Django TextField default value

In one of my tables I have a field game_fen_when_leave = models.TextField(). But it gives me an error "You are trying to add a non-nullable field 'game_fen_when_leave' to game without a default; we can't do that (the database needs something to populate existing rows)". Is it necessary for this field to have a default value? I saw an example without having a default.
Short answer
When creating a new model: No it is not
When adding it to an existing model: Yes it is
A bit more on the topic:
With the information given I guess your are about to add this new field to an existing table.
When adding a new non-nullable fields to an existing model you will need to provide a default value. This is because there might already be rows in that particular table and those would need a default value to populate this new field with. (I'm actually just repeating the error message here.)
In the example that you are referring:
The model is new and there cannot be existing rows that would need to be populated with default values. Therefore default value for the TextField is not needed.
Couple of possibilities
Remove and create the model from scratch: If you remove the table by migrations and create it again as a completely new table. You don't have to provide a default value as there cannot be existing rows.
Add a default value: Default value could simply be an empty string and that probably is the way to go.
By default Django TextField is a non-nullable yes. You have the power to change that, but it is not advised to do so:
https://docs.djangoproject.com/en/3.0/ref/models/fields/#null
If a string-based field has null=True, that means it has two possible
values for “no data”: NULL, and the empty string.

ManyToManyField references the 'id' field when using a custom primary key

I am using a custom primary key for a model that has a few ManyToManyFields. When I update the model and add an object to a ManyToManyField (using add(new_object)), I get an error signifying that its looking up the primary key using the id field (which perhaps exists in the intermediary table, but not in the model).
psycopg2.DataError: invalid input syntax for integer: "TL98GK"
LINE 1: ...WHERE ("placedir_place_place_categ"."place_id" = 'TL98GK' A...
I have been searching on SO for a while but havent been able to zero in the exact issue. I guess I may have to use custom through table for ManytoManyFields (as a punishment for using custom primary key) but I honestly dont want to go down that route.
Using Django 1.10 and Python 3
It seems the migration doesn't detect Foreign Key type changes well. It's a known bug. A workaround is mentioned here (i.e to explicitly change the field type to varchar(32)).
However, you may need to do more (like updating the constraints etc. on the table) depending on your use case. (For those interested to go that route, here is one example case with corresponding migration code).
(p.s I just decided to not use a custom primary key on models with manytomany fields)

What is the right way to add a field in the middle in django

I am new to Django.
I was trying to design a model in django. First I did with adding some fields, then I migrated the code. Later I found that I should have some other fields. I added some new fields, let say a CharField. Then while I was doing the migration, its showing error like you are trying to add a non-nullable field without a default. Can anybody tell should I add every time a default value to a new field OR Is there any other way to handle this?
You would need to add null=True to the field parameter that gives you this error in your model to allow for null values or give it a default value default=<value> and then re-run your migration

In solr search, solve the error: model 'X' has an empty model_attr 'company' and doesn't allow a default or null value

I am using solr search in django. When I am rebuilding the index with Solr, I get the following error:
model 'X' has an empty model_attr 'company' and doesn't allow a default or null value.
Does anyone know mistake I made, and how can I solve this issues.
It seems that you have some entries in your database where a mandatory field is left empty. This can be caused by adding a new mandatory field to an existing model so that existing instances of the model are invalidated due to the empty field.
If you want to enforce the field to have a value, you need to give the existing instances a value for the field. If you use the migration tool south it will give you an interactive prompt to fix the problem. Check out the "Data Migration" section of the south documentation.
If it is okay for you to leave the field optional, set blank=True (allows leaving the field blank) and null=True (sets NULL for empty field).
You need to add it as
indexes.CharField(model_attr='company', null=True)
and if you want to allow the blank then
add blank=True
indexes.CharField(model_attr='company', blank=True, null=True)

Passing a model instance, not __unicode__ method in django

I've got a django form that contains a join via a foreign key. This is a foreign key to a very large table. On the form, to prevent loading up a massive select that tends to crash browsers, I've got a jQuery autocomplete, which on each keystroke sends off the entered text. This text is then searched in the table and suitable results are returned to be displayed. The id is then passed to a hidden CharField when one is selected. This hidden CharField is the widget for the ForeignKey relation. When I try to save the form, I get an error that I need to be passing a model instance for the related model, which is fair enough. I can't work out how to do this however. I can take that id and do a model.objects.get(pk=id_from_form), but if I replace the POST data with the result of this, I still get an error as I'm just passing the __unicode__ method of the model. I'm sure there's something I'm missing, but I can't see what it is.
Thanks.
Instead of using a CharField to store the id, try using a ModelChoiceField with the widget set as a HiddenInput. The field definition in your form would look something like:
mymodel = forms.ModelChoiceField(widget=forms.HiddenInput, queryset=MyModel.objects.all())

Categories

Resources