Opened 8 years ago

Closed 8 years ago

#7266 closed (fixed)

"Not specified" states for BooleanField

Reported by: Valera Grishin Owned by: nobody
Component: contrib.admin Version: master
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: UI/UX:


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:

class DataClass(models.Model):
    name = models.CharField(max_length=100)

    class Admin:

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)

comment:1 follow-up: Changed 8 years ago by ubernostrum

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

Use NullBooleanField if you need three-value (yes/no/unknown) logic.

comment:2 in reply to: ↑ 1 Changed 8 years ago by Valera Grishin

  • Keywords NullBooleanField added
  • Resolution invalid deleted
  • Status changed from closed to 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):

class DataClass(models.Model):
    name = models.CharField(max_length=100)

    class Admin:

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 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 Changed 8 years ago by serialx

  • Component changed from Uncategorized to Admin interface

Isn't this more of a admin module problem than a django core model problem?

comment:4 Changed 8 years ago by Karen Tracey <kmtracey@…>

  • Keywords nfa-fixed added
  • Triage Stage changed from Unreviewed to 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 Changed 8 years ago by brosner

  • Resolution set to fixed
  • Status changed from reopened to closed

This is no longer a problem since the merge of newforms-admin in [7967].

Note: See TracTickets for help on using tickets.
Back to Top