Hello, I have a Django app with two models: User and ChatRoom. Each
user can be the owner of many chatrooms and can also link to his
friends's chatrooms. There's is no authentication in this little app,
the User model is just a custom model containing user ids.

This is an app that will work with Facebook and the TOS doesn't allow
storing user names, so I only can store user ids and then need to use
the Facebook API (I'm using the very useful PyFacebook for that) for
each user id to get the names on runtime.

My question is about adding some dynamic data to the text presented in
select lists and checkboxes generated by forms.ModelChoiceField and
forms.ModelMultipleChoiceField, I provide more details (and a few more
related questions) below.

This is the ChatRoom model.

class ChatRoom(models.Model):
  name = models.CharField(max_length=75)
  user = models.ForeignKey(User)

  def __unicode__(self):
    return self.name

I have also created two forms, I'm simplifying parts of the code to
make my point clear and focus just on what's causing me trouble:

1. The 'link' form, allows a user to link to a chatroom:

class LinkedChatRoomForm(forms.Form):
  queryset = ChatRoom.objects.all()
  chatroom = forms.ModelChoiceField(label="Choose a chatroom to link
to your profile", queryset=queryset)

With this form the user sees a select box and can choose a chatroom to
link to. The options in the select HTML are something like this:

<option value="13">AC/DC fans</option>
<option value="27">Metallica fan club</option>

That's chatroom.id for the values and chatroom.name for the text shown
to the user.

Because each chatroom is owned by some user I'd like to include that
user name in the list, so my HTML would be like:

<option value="13">AC/DC fans (owned by Rick)</option>
<option value="27">Metallica fan club (owned by Andrew)</option>

Let's remember I just have user ids in my User model and need to call
the Facebook API to get names for each user, so the question would be
how do I 'inject' those names in the form field.

2. The 'unlink' form, allows a user to check chatrooms that he doesn't
want to link to anymore:

I have to mention I have another model to keep track of which
chatrooms are linked to which users:

class LinkedChatRoom(models.Model):
  chatroom = models.ForeignKey(ChatRoom)
  user = models.ForeignKey(User)

I'm not sure why I didn't use models.ManyToManyField but this is
working so far for all the other parts of my app and don't think is
related to the problem I'm discussing here.

Now the form:

class MultipleLinkedChatRoomForm(forms.Form):

  user = get_current_user() # a function that returns the current user
object
  queryset = ChatRoom.objects.filter(linkedchatroom__user=user)

  chatrooms = forms.ModelMultipleChoiceField(
      label='Check the chatrooms to unlink from your profile',
      queryset=queryset,
      widget=forms.CheckboxSelectMultiple,
  )

This produces HTML like this:

<li><label><input type="checkbox" name="chatrooms" value="43" /> AC/DC
fans</label></li>
<li><label><input type="checkbox" name="chatrooms" value="29" />
Metallica fan club</label></li>

My requirement is the same as in the 'link' form. I'd like to include
the name of the user owning each chatroom, to produce this:

<li><label><input type="checkbox" name="chatrooms" value="43" /> AC/DC
fans (owned by Rick)</label></li>
<li><label><input type="checkbox" name="chatrooms" value="29" />
Metallica fan club (owned by Andrew)</label></li>

I'm not sure if I should subclass the form fields (I've been reviewing
ModelMultipleChoiceField definition in django/forms/models.py) or if
the magic should be in the querysets used for the form fields.

As I already mentioned this should work with Facebook as I only have
user ids in my database, so the querysets for these form fields have
no way of knowing the user names until I've called the Facebook API.

Maybe this would be way easier using forms.ChoiceField and
forms.MultipleChoiceField (the simpler versions not requiring a
queryset) but I've noticed that the choices in these fields don't get
updated when the records on the database change. That's why I'm using
the 'Model' versions of the fields. Or maybe I'm missing something
pretty important here?

Ok, if you've read until here you can clearly see I'm pretty confused
of how to proceed so any ideas are welcome :)

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