Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » how to define choices option per model's instance [RSS Feed]

#1 Dec. 2, 2010 06:53:37

Alex B.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

how to define choices option per model's instance


Hi!

I got such kind of problem.
I'd like to define the tuple(list) of choices option at the moment of
instance created on the fly
In the future these choices will not change.
Hence I've found just one way to do it - override __init__() of my model
and
pass there the particular predefined list of choices that depends on current
model's instance (in code - 'range_list')
But this approach seems doesn't work properly.
Would You be so kind to help with advice - how I can solve my task with
dynamical choices per model's instance.

---------------------------------------
RANGE_CHOICES = ()

class Something(models.Model):
def __init__(self, *args, **kwargs):
range = kwargs.pop('range_list', None)
super(Covenant, self).__init__(*args, **kwargs)
self._meta.get_field_by_name('range_marks')._choices = range

range_marks = models.IntegerField(choices = RANGE_CHOICES, verbose_name
= 'Marks of Something')
-----------------------------------------


Thanks in Advance!
Best Regards!
Alex

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Dec. 2, 2010 08:15:49

Cal L.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

how to define choices option per model's instance


Hi Alex,

I think this is what you might be looking for.

class Something(models.Model):
def __init__(self, *args, **kwargs):
if kwargs.has_key('range_marks'):
_choices = kwargs.get('range_marks')
del kwargs
else:
_choices = (('default', 'default'), )self.range_marks = models.IntegerField(choices = _choices,verbose_name = 'Marks of Something')super(Something, self).__init__(*args, **kwargs)

Let us know if it works :)

Cheers

Cal

On 02/12/2010 06:53, Alex Boyko wrote:Hi!

I got such kind of problem.I'd like to define the tuple(list) of choices option at the moment ofinstance created on the flyIn the future these choices will not change.Hence I've found just one way to do it - override __init__() of mymodel andpass there the particular predefined list of choices that depends oncurrent model's instance (in code - 'range_list')But this approach seems doesn't work properly.Would You be so kind to help with advice - how I can solve my taskwith dynamical choices per model's instance.---------------------------------------
RANGE_CHOICES = ()

class Something(models.Model):
def __init__(self, *args, **kwargs):
range = kwargs.pop('range_list', None)
super(Covenant, self).__init__(*args, **kwargs)
self._meta.get_field_by_name('range_marks')._choices = rangerange_marks = models.IntegerField(choices = RANGE_CHOICES,verbose_name = 'Marks of Something')-----------------------------------------


Thanks in Advance!
Best Regards!
Alex
--You received this message because you are subscribed to the GoogleGroups "Django users" group.To post to this group, send email to django-us...@googlegroups.com.To unsubscribe from this group, send email todjango-users+unsubscr...@googlegroups.com.For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.--
You received this message because you are subscribed to the Google Groups "Django
users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Dec. 2, 2010 19:01:02

Alex B.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

how to define choices option per model's instance


Hi Cal!

Thank you very much for your reply and advice, but unfortunately it doesn't
work. It seems like in __init__ method we can't re-assign the choices. Even
if I try:

class Something(models.Model):
def __init__(self, *args, **kwargs):
_choices = ((SomeValue, 'SomeString'), )
self.range_marks = models.IntegerField(choices = _choices,
verbose_name = 'Marks of Something')
super(Something, self).__init__(*args, **kwargs)

In admin page I see that my field has choices option initially defined in
global scope (I've showed that definition in my first letter).
Any suggestions about how I can assign choice attribute on the fly during
instance creation. In future It doesn't change but my instances have to have
different ranges in choices option.

Best Regards,
Alex



On 2 December 2010 10:15, Cal Leeming <
cal.leem...@simplicitymedialtd.co.uk> wrote:

> Hi Alex,
>
> I think this is what you might be looking for.
>
>
> class Something(models.Model):
> def __init__(self, *args, **kwargs):
> if kwargs.has_key('range_marks'):
> _choices = kwargs.get('range_marks')
> del kwargs
> else:
> _choices = (('default', 'default'), )
> self.range_marks = models.IntegerField(choices = _choices,
> verbose_name = 'Marks of Something')
> super(Something, self).__init__(*args, **kwargs)
>
> Let us know if it works :)
>
> Cheers
>
> Cal
>
>
> On 02/12/2010 06:53, Alex Boyko wrote:
>
>> Hi!
>>
>> I got such kind of problem.
>> I'd like to define the tuple(list) of choices option at the moment of
>> instance created on the fly
>> In the future these choices will not change.
>> Hence I've found just one way to do it - override __init__() of my model
>> and
>> pass there the particular predefined list of choices that depends on
>> current model's instance (in code - 'range_list')
>> But this approach seems doesn't work properly.
>> Would You be so kind to help with advice - how I can solve my task with
>> dynamical choices per model's instance.
>>
>> ---------------------------------------
>> RANGE_CHOICES = ()
>>
>> class Something(models.Model):
>> def __init__(self, *args, **kwargs):
>> range = kwargs.pop('range_list', None)
>> super(Covenant, self).__init__(*args, **kwargs)
>> self._meta.get_field_by_name('range_marks')._choices = range
>>
>> range_marks = models.IntegerField(choices = RANGE_CHOICES, verbose_name
>> = 'Marks of Something')
>> -----------------------------------------
>>
>>
>> Thanks in Advance!
>> Best Regards!
>> Alex
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To post to this group, send email to django-us...@googlegroups.com.
>> To unsubscribe from this group, send email to
>> django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com>
>> .
>> For more options, visit this group at
>>http://groups.google.com/group/django-users?hl=en.
>>
>

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Dec. 3, 2010 01:08:04

a.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

how to define choices option per model's instance


Here is an approach I have used. I can't tell from your question
whether this will help you or not.

class AccountEntryModelForm(ModelForm):

def __init__(self, organization=None, operation=None, *args,
**kwargs):
super(ModelForm, self).__init__(*args, **kwargs)
self.organization = organization
if self.organization:
account_entry_types =
AccountEntryType.objects.filter(organization=self.organization)
self.fields.choices =
self.fields.choices.extend()

HTH
Alan


On Dec 2, 2:00 pm, Alex Boyko <alex.kyr...@gmail.com> wrote:
> Hi Cal!
>
> Thank you very much for your reply and advice, but unfortunately it doesn't
> work. It seems like in __init__ method we can't re-assign the choices. Even
> if I try:
>
> class Something(models.Model):
>    def __init__(self, *args, **kwargs):
>        _choices = ((SomeValue, 'SomeString'), )
>        self.range_marks = models.IntegerField(choices = _choices,
> verbose_name = 'Marks of Something')
>        super(Something, self).__init__(*args, **kwargs)
>
> In admin page I see that my field has choices option initially defined in
> global scope (I've showed that definition in my first letter).
> Any suggestions about how I can assign choice attribute on the fly during
> instance creation. In future It doesn't change but my instances have to have
> different ranges in choices option.
>
> Best Regards,
> Alex
>
> On 2 December 2010 10:15, Cal Leeming <
>
>
>
>
>
>
>
> cal.leem...@simplicitymedialtd.co.uk> wrote:
> > Hi Alex,
>
> > I think this is what you might be looking for.
>
> > class Something(models.Model):
> >    def __init__(self, *args, **kwargs):
> >        if kwargs.has_key('range_marks'):
> >            _choices = kwargs.get('range_marks')
> >            del kwargs
> >        else:
> >            _choices = (('default', 'default'), )
> >        self.range_marks = models.IntegerField(choices = _choices,
> > verbose_name = 'Marks of Something')
> >        super(Something, self).__init__(*args, **kwargs)
>
> > Let us know if it works :)
>
> > Cheers
>
> > Cal
>
> > On 02/12/2010 06:53, Alex Boyko wrote:
>
> >> Hi!
>
> >> I got such kind of problem.
> >> I'd like to define the tuple(list) of choices option at the moment of
> >> instance created on the fly
> >> In the future these choices will not change.
> >> Hence I've found just one way to do it - override __init__() of my model
> >> and
> >> pass there the particular predefined list of choices that depends on
> >> current model's instance (in code - 'range_list')
> >> But this approach seems doesn't work properly.
> >> Would You be so kind to help with advice  - how I can solve my task with
> >> dynamical choices per model's instance.
>
> >> ---------------------------------------
> >> RANGE_CHOICES = ()
>
> >> class Something(models.Model):
> >>    def __init__(self, *args, **kwargs):
> >>        range = kwargs.pop('range_list', None)
> >>        super(Covenant, self).__init__(*args, **kwargs)
> >>        self._meta.get_field_by_name('range_marks')._choices = range
>
> >>    range_marks = models.IntegerField(choices = RANGE_CHOICES, verbose_name
> >> = 'Marks of Something')
> >> -----------------------------------------
>
> >> Thanks in Advance!
> >> Best Regards!
> >> Alex
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "Django users" group.
> >> To post to this group, send email to django-us...@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@google
> >> groups.com>
> >> .
> >> For more options, visit this group at
> >>http://groups.google.com/group/django-users?hl=en.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#5 Dec. 3, 2010 17:10:40

Cal L.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

how to define choices option per model's instance


Hi Alex,Sorry it took me a while to reply, only just saw the thread update.Yeah, thinking about it, the previous method probably wouldn't haveworked due to the way Django handles the db model objects.Okay, in this particular case, I would suggest re-thinking the way thisis being done. For example, rather than restricting this at the optionlevel, perhaps override the save() and add in some custom checkingthere. Athough the below code segment isn't perfect, it might be a goodstarting ground.class Something(models.Model):range_marks = models.IntegerField(choices = _choices, verbose_name ='Marks of Something')def __init__(self, *args, **kwargs):
if kwargs.has_key('range_marks'):
_choices = kwargs.get('range_marks')
del kwargs
else:
_choices = (('default', 'default'), )
self._choices_dict = dict(_choices)
self._choices = dict(_choices)
super(Something, self).__init__(*args, **kwargs)

def save(self, *args, **kwargs):if self.range_marks and notself._choices_dict.has_key(self.range_marks):raise Exception, "Invalid option" # replace this withwhatever exception is raised when django doesnt like your option, i cantremember it off the top of my head though, srysuper(Something, self).save(*args, **kwargs)

Hope this helps.

Cal

On 02/12/2010 19:00, Alex Boyko wrote:Hi Cal!Thank you very much for your reply and advice, but unfortunately itdoesn't work. It seems like in __init__ method we can't re-assign thechoices. Even if I try:class Something(models.Model):
def __init__(self, *args, **kwargs):
_choices = ((SomeValue, 'SomeString'), )self.range_marks = models.IntegerField(choices = _choices,verbose_name = 'Marks of Something')super(Something, self).__init__(*args, **kwargs)In admin page I see that my field has choices option initially definedin global scope (I've showed that definition in my first letter).Any suggestions about how I can assign choice attribute on the flyduring instance creation. In future It doesn't change but my instanceshave to have different ranges in choices option.Best Regards,
AlexOn 2 December 2010 10:15, Cal Leeming <cal.leem...@simplicitymedialtd.co.uk<mailto:cal.leem...@simplicitymedialtd.co.uk>> wrote:Hi Alex,

I think this is what you might be looking for.


class Something(models.Model):
def __init__(self, *args, **kwargs):
if kwargs.has_key('range_marks'):
_choices = kwargs.get('range_marks')
del kwargs
else:
_choices = (('default', 'default'), )
self.range_marks = models.IntegerField(choices = _choices,
verbose_name = 'Marks of Something')
super(Something, self).__init__(*args, **kwargs)

Let us know if it works :)

Cheers

Cal


On 02/12/2010 06:53, Alex Boyko wrote:

Hi!

I got such kind of problem.
I'd like to define the tuple(list) of choices option at the
moment of instance created on the fly
In the future these choices will not change.
Hence I've found just one way to do it - override __init__()
of my model and
pass there the particular predefined list of choices that
depends on current model's instance (in code - 'range_list')
But this approach seems doesn't work properly.
Would You be so kind to help with advice - how I can solve my
task with dynamical choices per model's instance.

---------------------------------------
RANGE_CHOICES = ()

class Something(models.Model):
def __init__(self, *args, **kwargs):
range = kwargs.pop('range_list', None)
super(Covenant, self).__init__(*args, **kwargs)
self._meta.get_field_by_name('range_marks')._choices
= range

range_marks = models.IntegerField(choices = RANGE_CHOICES,
verbose_name = 'Marks of Something')
-----------------------------------------


Thanks in Advance!
Best Regards!
Alex--You received this message because you are subscribed to theGoogle Groups "Django users" group.
To post to this group, send email to
django-users@googlegroups.com
<mailto:django-users@googlegroups.com>.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com
<mailto:django-users%2bunsubscr...@googlegroups.com>.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.--
You received this message because you are subscribed to the Google Groups "Django
users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » how to define choices option per model's instance [RSS Feed]

Board footer

Moderator control

Enjoy the 18th of August
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support