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
-~----------~----~----~----~------~----~------~--~---

Reply via email to