ModelForm Choices with session data

View: New views
2 Messages — Rating Filter:   Alert me  

ModelForm Choices with session data

by Keir-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hi,

I have recently been working on my first project in Django and have
encountered a small problem when using ModelForm. Here's my ModelForm
class

class MessageForm(forms.ModelForm):
        user_accounts = account.objects.filter(id=USER_ID)|
account.objects.filter(parent=USER_ID)
        accounts = forms.ModelMultipleChoiceField(user_accounts,
widget=forms.CheckboxSelectMultiple)

        class Meta:
                model = message
                exclude = ('status', 'primary_account', 'date_created',
'date_pending', 'date_set', 'twitter_status_id')

This works really well when I have the USER_ID set at the module
level. The problem I get is when I need to create the user_accounts
based on a session variable. We don't actually use the in built Django
user auth so it's just a simple session at the moment as we
authenticate against the Twitter API.

After creating the view function for the form it renders as expected
but with the full list of user accounts and not the filtered accounts.
I understand that the MessageForm class has no concept of the
request.session so substituting the USER_ID constant for that will not
work. However I tried a number of ways of trying to override the
user_accounts in the view function but couldn't get it to work.

What I would like to be able to do is set the user_accounts queryset
from the view function and then pass that data into the CHOICES
argument for the accounts widget. I have read about form-base_fields
but just can't seem to get it to work.

Can you help? Thanks.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django developers" group.
To post to this group, send email to django-developers@...
To unsubscribe from this group, send email to django-developers-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: ModelForm Choices with session data

by Tom Tobin-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On Sat, Jul 5, 2008 at 3:10 AM, Keir <keirwhitaker@...> wrote:
> I have recently been working on my first project in Django and have
> encountered a small problem when using ModelForm. Here's my ModelForm
> class

You're on the wrong list.  ^_^  For support questions, you want django-users.

The terms "users" and "developers" in this case are probably a tad
confusing; everyone *using* Django is a developer of some sort, so
many naturally write "django-developers" for help, not realizing that
the term is meant to refer to developing Django *itself*.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django developers" group.
To post to this group, send email to django-developers@...
To unsubscribe from this group, send email to django-developers-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

LightInTheBox - Buy quality products at wholesale price