Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » select multiple without using foreignkey [RSS Feed]

#1 June 2, 2010 02:00:12

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

select multiple without using foreignkey


I have a models that represents an event.

I want it to have a field that represents the days of the week that
the event occurs. This is easy if all you need to be able to do is be
able to select one day, but if you want to select multiple, the right
way seems to be to create a DayOfWeek object, create 7 instances in
your db, the use ManyToMany relations. To me that just seems wrong.
I'd rather store it as a string like "WFS" or "MWF". Is there an easy
way to do this in django?

--
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 June 2, 2010 14:22:52

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

select multiple without using foreignkey


On Jun 2, 3:00 am, HARRY POTTRER <cp368...@ohio.edu> wrote:
> I have a models that represents an event.
>
> I want it to have a field that represents the days of the week that
> the event occurs. This is easy if all you need to be able to do is be
> able to select one day, but if you want to select multiple, the right
> way seems to be to create a DayOfWeek object, create 7 instances in
> your db, the use ManyToMany relations. To me that just seems wrong.
> I'd rather store it as a string like "WFS" or "MWF". Is there an easy
> way to do this in django?

You may want to think about storing the weekdays as numeric
equivalents e.g. 0 = Sunday, 1 = Monday etc.

Then you could use a CommaSeparatedIntegerField to store any combo you
need - see:http://docs.djangoproject.com/en/dev/ref/models/fields/#commaseparatedintegerfieldhttp://stackoverflow.com/questions/1208698/how-do-i-use-commaseparatedintegerfield-in-djangohttp://www.pubbs.net/200908/django/9885-how-to-use-selectmultiple-widget-with-commaseparatedintegerfield-.htmlAlternatively, you write a custom field - see:http://www.davidcramer.net/code/181/custom-fields-in-django.html--
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 June 2, 2010 15:16:46

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

select multiple without using foreignkey


On Jun 2, 2:00 am, HARRY POTTRER <cp368...@ohio.edu> wrote:
> I have a models that represents an event.
>
> I want it to have a field that represents the days of the week that
> the event occurs. This is easy if all you need to be able to do is be
> able to select one day, but if you want to select multiple, the right
> way seems to be to create a DayOfWeek object, create 7 instances in
> your db, the use ManyToMany relations. To me that just seems wrong.
> I'd rather store it as a string like "WFS" or "MWF". Is there an easy
> way to do this in django?

Ages ago I wrote a snippet with a model field and form field to deal
with this. It should still work. See:http://djangosnippets.org/snippets/1200/--
DR.

--
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 June 8, 2010 14:42:47

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

select multiple without using foreignkey


On Wed, Jun 2, 2010 at 3:16 PM, Daniel Roseman <dan...@roseman.org.uk> wrote:
> On Jun 2, 2:00 am, HARRY POTTRER <cp368...@ohio.edu> wrote:
>> I have a models that represents an event.
>>
>> I want it to have a field that represents the days of the week that
>> the event occurs. This is easy if all you need to be able to do is be
>> able to select one day, but if you want to select multiple, the right
>> way seems to be to create a DayOfWeek object, create 7 instances in
>> your db, the use ManyToMany relations. To me that just seems wrong.
>> I'd rather store it as a string like "WFS" or "MWF". Is there an easy
>> way to do this in django?
>
> Ages ago I wrote a snippet with a model field and form field to deal
> with this. It should still work. See:
>http://djangosnippets.org/snippets/1200/> --
> DR.
>

You can always kick it old school, you're talking about a bit field:

MONDAY = 1
TUESDAY = 1 << 1
WEDNESDAY = 1 << 2
THURSDAY = 1 << 3
FRIDAY = 1 << 4
SATURDAY = 1 << 5
SUNDAY = 1 << 6

class Foo(models.Model):
days_of_week = models.PositiveIntegerField(default=0)

foo = Foo.objects.create(days_of_week=MONDAY|WEDNESDAY)

A more complete implementation could define a bitfield field type, and
have it handle things like doing bit level DB queries (something like
Foo.objects.filter(days_of_week__has=WEDNESDAY)).

Cheers

Tom

--
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 June 19, 2010 05:35:22

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

select multiple without using foreignkey


> Ages ago I wrote asnippetwith a model field and form field to deal
> with this. It should still work. See:http://djangosnippets.org/snippets/1200/> --
> DR.

I'm using this snippet like so:

MONTHS =

class MyModel(models.Model):
months = MultiSelectField(max_length=30, choices=MONTHS,
help_text="What months of the year do you travel this route?")

And it works fine except for when I do MyModel.get_months_display() it
returns "1,2,3,4,5" instead of "January, Februrary, March, ..." Is
this by design, or am I using the snippet wrong?

--
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
  • » select multiple without using foreignkey [RSS Feed]

Board footer

Moderator control

Enjoy the 14th of December
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