Django ModelForm won't allow Null value for required field? - python

I have a Model with this field...
fulfillment_flag = models.CharField(pgettext_lazy('Delivery group field', 'Fulfillment Flag'), max_length=255,
null=True, choices=FULFILLMENT_FLAG_CHOICES)
And here is my form...
class FulfillmentFlagForm(forms.ModelForm):
class Meta:
model = DeliveryGroup
fields = ['fulfillment_flag', ]
def clean_fulfillment_flag(self):
return self.cleaned_data['fulfillment_flag'] or None
I have an HTML select drop down that has a blank value option at the top. Every time I select the blank option and click Save, form will not save it as a Null value on my model. It'll save any of the other fields though, just not the blank one. And it will tell me that the field is required as well.
How can I tell the form to just save the blank value as Null?

https://docs.djangoproject.com/en/1.8/ref/models/fields/#null
Avoid using null on string-based fields such as CharField and TextField because empty string values will always be stored as empty strings, not as NULL. If a string-based field has null=True, that means it has two possible values for “no data”: NULL, and the empty string. In most cases, it’s redundant to have two possible values for “no data;” the Django convention is to use the empty string, not NULL.
For both string-based and non-string-based fields, you will also need to set blank=True if you wish to permit empty values in forms, as the null parameter only affects database storage (see blank).
Do blank=True instead and save "" instead of None.

Related

Error while changing a Char field to Array Field in Django

I am trying to change a existing CharField in a django model (it also allows null and contains null values in db currently)
now I am trying to change it to an Array Char Field, but it throws the below error
"django.db.utils.IntegrityError: column "tags" contains null values"
From
tags= models.CharField(max_length=255, blank=True, null=True)
To
tags = ArrayField(models.CharField(max_length=255, blank=True, null=True))
I have selected option 2 while before running migrate
In this option 2 you selected, you were asked to select a default value yourself, right?
Add a default attribute to your model's field. The existing null values will be turned into that value, for instance:
tags = ArrayField(models.CharField(max_length=255, blank=True, null=True, default=[]))

Django - filter model

I have a model with a field that is a list.
For example:
mymodel.sessions = [session1, session2]
I need a query to get all mymodels that session1 is exist their sessions.
The model`s field looks like that
sessions = models.ForeignKey("Session", related_name="abstracts",
null=True, blank=True)
Thank you !
You can use reverse lookups that go back along the foreign key to query for values in the related model.
MyModel.objects.filter(sessions__id=1)
That will filter all MyModels that have a foreign key to a session with an id of 1.
For more information see https://docs.djangoproject.com/en/1.10/topics/db/queries/#lookups-that-span-relationships
From the Django docs for filter:
filter(**kwargs)
Returns a new QuerySet containing objects that match the given lookup parameters.
You can filter on a ForeignKey relationship using the id, if you have it:
The field specified in a lookup has to be the name of a model field. There’s one exception though, in case of a ForeignKey you can specify the field name suffixed with _id. In this case, the value parameter is expected to contain the raw value of the foreign model’s primary key.
In your instance, this would like the following:
mymodel.objects.filter(sessions_id=4)
If you want to filter on any other field in the Sessions model, simply use a double underscore with the field. Some examples:
mymodel.objects.filter(sessions__name='session1')
mymodel.objects.filter(sessions__name__contains='1')

How to show a message in odoo when a field is not selected

I have two fields. One field depends on other field. Second field have a domain which check condition based on the first field's selected value. So if the user do not selected the first field and trying to select the second field, then it should show a message that, select first field. [I tried using required=True but it will give message after when I am trying to save the record.]
roomType = fields.Selection([('meeting','Meeting Room'),('discussion','Discussion Room')])
meeting_room=fields.Many2one(comodel_name='mroom',string="select the room",required=True,domain='[("roomType","=",roomType)]')
you can use alternative solution.
Initially make meeting_room field invisible or readonly. Now use attrs on roomType field which will make meeting_room visible. in this way you will have roomType value for domain of meeting_room field.

Can I change the type of Model Field?

Say, I have a model with this field:
is_retired = models.NullBooleanField()
It has never been set for any of the records i the database, so I think all the records will have a null value for this field.
And now want to change it to a string field:
is_retired = models.CharField(max_length=50)
I am using Django 1.7 and ran makemigrations which seemed to go fine, said something about altered field. However, when I actually run the migrate command it fails with
django.db.utils.IntegrityError: column "is_retired" contains null values
How can I resolve this?
If you want to enable null/empty values, change it to:
is_retired = models.CharField(max_length=50, null=True, blank=True)
You might also want to change the null values to empty strings ('') in another migration.

Django Model Field Default to Null

I need to have my Django application allow me to have a default value of NULL set for a certain model field. I've looked over the null, blank, and default parameters, but it's not very clear what combination of the three I need to use to get the desired effect. I've tried setting default=NULL but it threw an error. If I specify blank=True, null=True and no default, will it just default back to NULL come runtime?
Try default=None. There is no NULL in python.
If you specify null=True on the model field then the value will be stored as NULL in the database if the user does not provide a value.
blank=True
allows you to input nothing (i.e "", None) and keep it empty.
null=True
means the database row is allowed to be NULL.
default=None
sets the field to None if no other value is given.

Categories

Resources