Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

define a required field with no default #25

Closed
YAmikep opened this issue Aug 21, 2015 · 9 comments
Closed

define a required field with no default #25

YAmikep opened this issue Aug 21, 2015 · 9 comments
Assignees

Comments

@YAmikep
Copy link

YAmikep commented Aug 21, 2015

The code automatically sets the first choice as the default value: https://github.com/5monkeys/django-enumfield/blob/master/django_enumfield/db/fields.py#L16-L17

I think this behavior is misleading because if someone defines a field with no default, he expects the field to have no default assigned so that an error is raised if the field is also required.

Right now, it's impossible to have a required field (null=False) with no default that would work as expected in the admin.

enum.EnumField(STATUSES, blank=True, null=False)

Setting default=None obviously doesn't work as the whole code assumes there is a value.

@ConorMcGee
Copy link

I agree this behaviour is unintuitive. Definitely likely to lead to bugs.

@jturmel
Copy link

jturmel commented Oct 23, 2015

I just ran into this same issue, I solved it for now by setting default=models.NOT_PROVIDED

from django.db import models
from django_enumfield import enum

enum.EnumField(STATUSES, default=models.NOT_PROVIDED)

It's not ideal, but the clearest solution I could come up without modifying the library

@avinassh
Copy link

wow this is very misleading and README should clarify.

@andreif
Copy link
Contributor

andreif commented Nov 16, 2015

Hey guys, I see your concern. We are used to have the first member as UNDEFINED = 0 or INITIAL_STATE = 0 etc. so it could catch cases where app misses to set it. It may not fit all use cases I agree. We are now discussing if Enum should have an ability to define the default value by itself or it's just EnumField that should define the default even if the same Enum is used in dozens of places across an application. Let me know what you would prefer.

@avinassh
Copy link

As of now a note on README is fine :)

@jturmel
Copy link

jturmel commented Nov 17, 2015

It'd be nice if they both support a default probably, I can see the case where you'd want to override the default on a re-used Enum... if it only had to be one though it'd be cleaner if it was on the Enum I guess.

The reason UNDEFINED = 0 doesn't work for us is that on some Enum the numerical value maps to something significant like an ISO value, etc.

@andreif andreif self-assigned this Feb 10, 2017
@Swamii
Copy link
Contributor

Swamii commented Nov 19, 2019

The default value isn't automatically set anymore (since #48).

@clovis1122
Copy link

Got the same issue using version 2. @jturme's solution did not work for me so I had to patch the library to keep things working:

from django_enumfield import enum

class PatchedEnum(enum.Enum):
    @classmethod
    def _missing_(cls, value):
        if cls.__default__ is not None:
            return cls.default()
        if value is None:
            return None
        return super()._missing_(cls, value)

# Then instead of inheriting from enum.Enum, use PatchedEnum.

@Swamii
Copy link
Contributor

Swamii commented Dec 12, 2019

@clovis1122 I'm not sure I follow. What is the issue you want to solve exactly? An example of a model would help me understand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants