Re: Limiting Foreign Key choices to members of a certain group using generic views

2007-11-02 Thread RajeshD

>
> # run with this form following the examples in the newforms
> documentations.

You can also change the domain "choices" after the form class is
created for you by modifying the form class's base_fields as I see you
have done in your post in another thread here:

http://groups.google.com/group/django-users/msg/b2c960e64a06b666

The callback method pattern I mentioned above is still nice to know as
you can do many other customizations that way as the form class is
being created instead of later.

Cheers,
-Rajesh D


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



Re: Limiting Foreign Key choices to members of a certain group using generic views

2007-11-02 Thread RajeshD



On Nov 2, 4:02 pm, rm <[EMAIL PROTECTED]> wrote:
> On Nov 2, 3:54 pm, RajeshD <[EMAIL PROTECTED]> wrote:
>
> > When you acquire a Customer newform instance in your view, you will be
> > able to modify the choices attribute of its domain field so that these
> > choices only contain the relevant domains that you mentioned you can
> > trivially derive knowing the request.user.
>
> Could you give an example of how to do this?  That sounds like what I
> am after as well.

Sure. But I will assume that you've read the newforms documentation or
will be reading it to continue with the starter code below:

# First create a form call back that will allow you
# to have our custom choice field on the domain field
class CustomDomains(object):
def __init__(self, queryset):
self.queryset = queryset

def __call__(field, **kwargs):
if field.name == 'domain':
# the following single line is the whole point \
# of the solution we are building
return forms.ModelChoiceField(queryset=self.queryset)
else:
return field.formfield(**kwargs)

# The following goes into your view
# Scroll down to "def contact_edit(request, msg_id)" in the newforms
docs for
# the general idea of this view.
# http://www.djangoproject.com/documentation/newforms/#form-for-instance

# create a queryset that contains
# your domain choices for the request.user

your_list_of_reseller_ids = [left for you to populate]
valid_domains =
Domain.objects.filter(reseller__id__in=your_list_of_reseller_ids)
custom_domains_callback = CustomDomains(queryset=valid_domains)

if your_customer_instance is not None: # we are in update mode
CustomerForm = forms.form_for_instance(your_customer_instance,
formfield_callback=custom_domains_callback)
else: # we are creating a new customer
CustomerForm = forms.form_for_model(Customer,
formfield_callback=custom_domains_callback)

form = CustomerForm()

# run with this form following the examples in the newforms
documentations.

Note: I have not tested the above for errors but have used similar
patterns in my own code with no problems. Also, please mind the
indentation. If GoogleGroups screws it up, I hope you can put it back
together.


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



Re: Limiting Foreign Key choices to members of a certain group using generic views

2007-11-02 Thread rm



On Nov 2, 3:54 pm, RajeshD <[EMAIL PROTECTED]> wrote:
> When you acquire a Customer newform instance in your view, you will be
> able to modify the choices attribute of its domain field so that these
> choices only contain the relevant domains that you mentioned you can
> trivially derive knowing the request.user.

Could you give an example of how to do this?  That sounds like what I
am after as well.


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



Re: Limiting Foreign Key choices to members of a certain group using generic views

2007-11-02 Thread RajeshD


>
> I'm after a way of limiting what choices are populated into a drop-down
> box from a foreign key field when I'm using the generic create/update views:

I would recommend not using the generic create/update views as those
views use old forms and manipulators which are going away in favor of
"newforms".

Take a look at the newforms documentation here:
http://www.djangoproject.com/documentation/newforms/

In particular, read the sub-sections on "form_for_model" and
"form_for_instance" which will let you get handy newforms instances
that can respectively create and update a Customer record.

>
> What I am trying to achieve is to be able to limit the list of domains
> that a reseller can apply to their customer objects to domains that they
> have control of:

When you acquire a Customer newform instance in your view, you will be
able to modify the choices attribute of its domain field so that these
choices only contain the relevant domains that you mentioned you can
trivially derive knowing the request.user.

> I think I need to be able to resolve the logged in username into a list
> of groups (which I can do with request.user.groups) and then resolve
> that into the list of resellers and then resolve that into a list of
> domains. I don't think I can access the request object from the model can I?

Its possible to do this through a hack that  (http://lukeplant.me.uk/
blog.php?id=1107301634) but it's not a good practice to closely couple
your view code (i.e. the request object) with your model code. You
won't need to resort to this anyway if you are willing to write your
own newforms based view suggested above.

-Rajesh D.


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