Closed kavdev closed 7 years ago
I'll add tests if this is something you're interested in merging.
I've had similar needs so far, but the problem is that you currently can only access the ChoiceItem, so accessing that has to become public API as well. I don't see any problems with that though.
Thanks for your contributions, I'm not near a computer until Monday, so I can't properly merge and release yet.
On Jun 16, 2017 02:32, "Alexander Kavanaugh" notifications@github.com wrote:
I'll tests if this is something you're interested in merging.
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/bigjason/django-choices/pull/52#issuecomment-308899371, or mute the thread https://github.com/notifications/unsubscribe-auth/AFQ01ibHRBIi24F13YKBNau6Y8u3IWeHks5sEcozgaJpZM4N74p6 .
No worries! I'll be committing tests and packaging updates in a bit. Let me know how you want to handle opening up the fields API and I'll be happy to squeeze that in as well.
hey @kavdev, I found some time to take a look at this, and I've got a couple things that I'm leaning towards to. Happy to discuss if you disagree on some aspects!
The ChoiceItem
modifications:
I think we shouldn't just setattr
the custom properties, I'd rather keep them a bit 'protected' in a self._extras
attribute. I think this doesn't pollute the namespace too much, and it makes it easier to keep track on what exactly was passed in as custom props (and useful for item 2.). So the init would become more like this:
def __init__(self, value=sentinel, label=None, order=None, **extra):
self.value = value
self.label = label
self._extra = extra
if order is not None:
self.order = order
else:
ChoiceItem.order += 1
self.order = ChoiceItem.order
Since the introduction of arbitratry props may complicate things, I think it'd be wise to implement the __repr__
method to make debugging easier:
def __repr__(self):
extras = " ".join([
"{key}={value!r}".format(key=key, value=value)
for key, value in self._extra.items()
])
return "<{} value={!r} label={!r} order={!r}{extras}>".format(
self.__class__.__name__, self.value,
self.label, self.order,
extras=" " + extras if extras else ""
)
The DjangoChoices
class:
Essentially this would be 'opening up' the _fields
api, but I'd make it a class method:
@classmethod
def get_choice(cls, value):
"""
Return the underlying :class:`ChoiceItem` for a given value.
"""
attribute_for_value = cls.attributes[value]
return cls._fields[attribute_for_value]
The details are in the cls.attributes
, which may throw an exception if duplicate values are used.
Example usage would then be:
>>> choice_item = MyChoices.get_choice(MyChoices.foo)
Thoughts?
@sergei-maertens I like it! I'll hopefully get to it this weekend. I'll create a separate PR for the get_choice
method
If you could put it all in the existing PR that would make things a bit more contained and I'd prefer that :-)
On Jun 21, 2017 23:56, "Alexander Kavanaugh" notifications@github.com wrote:
@sergei-maertens https://github.com/sergei-maertens I like it! I'll hopefully get to it this weekend. I'll create a separate PR for the get_choice method
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/bigjason/django-choices/pull/52#issuecomment-310216718, or mute the thread https://github.com/notifications/unsubscribe-auth/AFQ01ucGBPsEU-IDuz5KWfTqtWEv-fPTks5sGZGcgaJpZM4N74p6 .
@sergei-maertens no problem. whatever works best for you.
@sergei-maertens thinking about it, the "_extras" protection seems more of a hindrance than a benefit.
If I set:
class MyChoices(DjangoChoices):
choice_item = ChoiceItem(value=0, label="test", help_text="help!")
I'd expect to be able to use
>>> choice_item = MyChoices.get_choice(MyChoices.choice_item)
>>> choice_item.help_text
'help!'
That's more intuitive and readable than
>>> choice_item._extras["help_text"]
Looks like I forgot a def __getattr__(self):
in my sample code!
On Jun 28, 2017 01:01, "Alexander Kavanaugh" notifications@github.com wrote:
@sergei-maertens https://github.com/sergei-maertens thinking about it, the "_extras" protection seems more of a hindrance than a benefit.
If I set:
class MyChoices(DjangoChoices): choice_item = ChoiceItem(value=0, label="test", help_text="help!")
I'd expect to be able to use
choice_item = MyChoices.get_choice(MyChoices.choice_item)>>> choice_item.help_text'help!'
That's more intuitive and readable than
choice_item._extras["help_text"]
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/bigjason/django-choices/pull/52#issuecomment-311509889, or mute the thread https://github.com/notifications/unsubscribe-auth/AFQ01iGnSLKaso2XnipRajfTfs-QF0Qxks5sIYnRgaJpZM4N74p6 .
Looks like I forgot a
def __getattr__(self):
in my sample code!
👍 I'll add it
@sergei-maertens coveralls doesn't seem to enjoy rebasing :)
it does seem pretty chatty, yes :P
Thanks for your contribution! Only thing missing is a documentation update. Will you do that as well, or do I need to follow up on that? I'm merging this either way - if you decide to tackle docs, you can submit a new PR for that :-)
@sergei-maertens I can add it to my list, but I'm pretty swamped; it might be a month or so until it reaches the top
Alright, I'll handle it!
On Jul 11, 2017 20:15, "Alexander Kavanaugh" notifications@github.com wrote:
@sergei-maertens https://github.com/sergei-maertens I can add it to my list, but I'm pretty swamped; it might be a month or so until it reaches the top
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/bigjason/django-choices/pull/52#issuecomment-314528365, or mute the thread https://github.com/notifications/unsubscribe-auth/AFQ01qPHTqb0o-1nRePtLjifg8rRLqNVks5sM7vEgaJpZM4N74p6 .
Thanks!
@kavdev 1.6.0 has just been released on PyPI
It's not common behavior, but we have a model that validates data based on a chosen data label. Instead of putting the validator into a dictionary and accessing it ad-hoc, we'd rather just set it on the choice and access it.
This implements the ability to add custom attributes to a choice.