views:

43

answers:

1

Hi All,

I have a django model roughly as shown below:

class Event(db.Model):
   creator = db.ReferenceProperty(User, required= True)
   title = db.TextProperty(required = True)
   description = db.TextProperty(required = True)

class Ticket(db.Model):
   user = db.ReferenceProperty(User, required = True)
   event = db.ReferenceProperty(Event, required = True)
   total_seats = db.IntegerProperty(required = True,default=0)
   available_seats = db.IntegerProperty(required = True,default=0)

Now I want to create a form from this model which should contain the events which are own by the logged in users only. Currently it shows a drop down with all the events in it.

Is it possible with django forms? I am working on google app engine.

Please suggest.

+1  A: 

This is how I'd go about it if this were a pure Django application (rather than app engine). You may perhaps find it useful.

The key is to override the __init__() method of your ModelForm class to supply the currently logged in user instance.

# forms.py
class TicketForm(forms.ModelForm):
    def __init__(self, current_user, *args, **kwargs):
        super(TicketForm, self).__init__(*args, **kwargs)
        self.fields['event'].queryset = Event.objects.filter(creator = 
             current_user)

You can then supply the user instance while creating an instance of the form.

ticket_form = TicketForm(request.user)
Manoj Govindan
Thanks Manoj. It worked with some small changes.
anand
anand, would you mind posting what those "small changes" were that made it work? Thanks!
Aaron
Just the change in the query to fetch the result. like: Event.all().filter('creator =',current_user) will work with google app engine. slight difference in syntax in pure django and app engine.
anand