Opened 17 years ago
Closed 16 years ago
#7266 closed (fixed)
"Not specified" states for BooleanField
Reported by: | Valera Grishin | Owned by: | nobody |
---|---|---|---|
Component: | contrib.admin | Version: | dev |
Severity: | Keywords: | BooleanField NullBooleanField nfa-fixed | |
Cc: | Triage Stage: | Fixed on a branch | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
BooleanField needs additional states "Not specified" which means that field wasn't supplied by user when editing ForeignKey'ed model with core=True
Following is an intentionally simplified (for the purpose of demonstration) version of models I need to use in my project:
!#python class DataClass(models.Model): name = models.CharField(max_length=100) class Admin: pass class BooleanAttribute(models.Model): owner = models.ForeignKey(to=DataClass, core=True, edit_inline=models.TABULAR) class Admin: min_num_in_admin = 3
When editing the DataClass model via Django's default admin interface it will (of course) display 3 (according to min_num_in_admin) BooleanAttibute models too. Submitting the form will cause all three BooleanAttributes to be added to the newly created DataClass instance. And there is no way to say how many of them really need to be saved.
My proposition is too enhance BooleanField with optional second checkbox that will indicate whether value is given or not. And only if it is on then the actual value of BooleanField to be considered.
I'd like to provide patch for this issue but have no idea of where to look at. Any help - hints, suggestions, etc - will be greatly appriciated.
Change History (5)
follow-up: 2 comment:1 by , 17 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
comment:2 by , 17 years ago
Keywords: | NullBooleanField added |
---|---|
Resolution: | invalid |
Status: | closed → reopened |
Replying to ubernostrum:
Use
NullBooleanField
if you need three-value (yes/no/unknown) logic.
Ah, sorry, I gave the wrong models in the example above. Anyway, three-value logic isn't what I meant.
Let me describe the issue once again. The correct models are as follows (added "value" attribute to BooleanAttribute model):
!#python class DataClass(models.Model): name = models.CharField(max_length=100) class Admin: pass class BooleanAttribute(models.Model): owner = models.ForeignKey(to=DataClass, core=True, edit_inline=models.TABULAR) value = models.BooleanField(core=True) # or NullBooleanField class Admin: min_num_in_admin = 3
Note core=True in the "value" attribute of BooleanAttribute model.
Now, pretetend you're adding new DataClass via stadard admin interface. You will see one field for DataClass.name and three checkboxes for BooleanAttribute.value. In case of NullBooleanField there will be listbox.
Assume the instance you're editing only needs one BooleanAttribute. This means "create one BooleanAttribute and ignore two others". But there is no way to say that. That is if you submit the form the database will grow with one row in the DataClass table and three rows in the BooleanAttribute table.
So the point of this ticket is to have ability to differentiate the situation when BooleanField (or NullBooleanField) is the core (core=True) field in the model which ForeignKey'ed to another model, and there is no need to create the instance of the former model while creating the instance of the latter model.
As you can see, this is different to having "unknown" as one of the options for the NullBooleanField which actually stores the field with NULL value.
Hope it's clear enough this time. Let me know if there is any idea of how to fix it so I can work on patch. Thanks.
comment:3 by , 16 years ago
Component: | Uncategorized → Admin interface |
---|
Isn't this more of a admin module problem than a django core model problem?
comment:4 by , 16 years ago
Keywords: | nfa-fixed added |
---|---|
Triage Stage: | Unreviewed → Fixed on a branch |
I'm going to say this is 'fixed on a branch' because the behavior of adding multiple of the inline-edited objects is no longer present in newforms-admin.
Note, though, that for these specific models, the behavior might still seem a bit odd. (But I think the models are a bit odd.) If the only field besides the ForeignKey you have in your inline-edited model is a BooleanField, then the only instances of the inline-edited model you can create from the parent's edit page are those where the values of the BooleanField are True. You cannot create one with False or Unknown (in the case of NullBooleanField) because these values are considered 'empty', and if the BooleanField is the only field in the inline-edited object, then the whole inline-edited row appears to be empty and it is not added.
Therefore, if you really need to have this kind of related object, you will need to create the False/Unknown values via their own admin page, not via editing the related parent object. You can then see (and delete, if you like) the related objects on the parent's change page. You just can't create them there.
comment:5 by , 16 years ago
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
This is no longer a problem since the merge of newforms-admin in [7967].
Use
NullBooleanField
if you need three-value (yes/no/unknown) logic.