Admin fields for contained members

2006-12-06 Thread JMCameron

>From tutorial, part 2, the following type of code

class Poll(models.Model):
...
class Admin:
 fields = (  )

is used to control how the admin interface displays the Poll objects
(controlling which items appear on separate lines, etc).

How can I control how the contained objects are displayed?  (eg, the
Choice items referring to this particular Poll)For instance,
suppose I wanted to have the Choice question and the vote count items
on separate lines?

I tried adding the "class Admin: fields..." code to Choice, but that
didn't make any difference to the compound display that the admin
interface creates for creating/editing Poll objects.

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



Problem with custom forms and related objects

2006-12-29 Thread JMCameron


I'm having a problem with custom forms and models with related objects.
I've created a simplified test case that illustrates the problem.
Here is the model file (a room contains several chairs):

-- model.py -
class Room(models.Model):
   name = models.CharField(maxlength=30, core=True)
   class Admin:
   pass

class Chair(models.Model):
   room = models.ForeignKey(Room, edit_inline=models.TABULAR,
num_in_admin=3)
   style = models.CharField(maxlength=30, core=True)
   class Admin:
   pass


I've defined a custom form and manipulator:

-- views.py --
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django import forms

from pytest.jtest.models import Room, Chair

class RoomForm(forms.Manipulator):
   def __init__(self, num_chairs=3):
   self.num_chairs = num_chairs

   # The fields for Room
   self.fields = [ forms.TextField(field_name='name', length=20,
maxlength=30, is_required=True), ]

   # Add extra fields for potential related chairs
   for i in range(num_chairs):
   self.fields.append(
forms.TextField(field_name='chair.%d.style' % i, length=20,
maxlength=30, is_required=False) )

   def save(self, new_data):
   # Add the main Room object
   r = Room( aname  = new_data['aname'] )
   r.save()

   # Save any related chair objects containing data
   for i in range(self.num_chairs):
   if new_data['chair.%d.style' % i]:
   c = Chair( room = r.id, style =
new_data['chair.%d.style' % i] )
   c.save()
   return r

def create_room(request, template_name):
   manipulator = RoomForm( num_chairs = 3 )  # <--- A: THIS DOES NOT
WORK (no fields for chairs)
   # manipulator = Room.AddManipulator()   # <--- B: THIS WORKS
   if request.POST:
   new_data = request.POST.copy()
   errors = manipulator.get_validation_errors(new_data)
   if not errors:
   manipulator.do_html2python(new_data)
   new_itinerary = manipulator.save(new_data)
   return HttpResponseRedirect("/")
   else:
   errors = new_data =  {}
   form = forms.FormWrapper(manipulator, new_data, errors)
   return render_to_response(template_name, { 'form' : form },
 context_instance=RequestContext(request))
-

And here is what the template looks like:

-- create_room.html --
Create Room
Room name:  {{ form.name }}
Add Chairs
{% for the_chair in form.chair %}

 Chair
Style: {{ the_chair.style }}
{% endfor %}
---
(yes, I've left out the form HTML elements, for simplicity)

When I try this with the line B (in create_room()), this all works and
shows the fields for the chairs because I'm using Room.AddManipulator.
If I define my own manipulator (as shown above) using line A instead
of line B, the template does not show the fields for the chairs.  In
other words, the {% for the_chair in form.chair %} statement does not
work or finds nothing for form.chair.I want to use my own custom
manipulator to have more control over the fields (sizes, etc).   I had
to guess about how to add the extra fields for the chairs...

Suggestions?  Thanks!

 -Jonathan


--~--~-~--~~~---~--~~
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: Problem with custom forms and related objects

2006-12-30 Thread JMCameron


Thanks ElGranAzul,  my django code is up to date (via svn), so
'oldforms' is equivalent to 'forms' (the __init__.py files are the
same).   So using 'from django import oldforms as forms' may be better
form (no pun intended!), but will not fix the problem I'm trying to
figure out.

Both RoomForm and Room.AddManipulator add fields to the
form/manipulator with names like this: name, chair.0.style,
chair.1.style, chair.2.style.

Somehow, the AddManipulator does a bit of magic to make {% for
the_chair in form.chair %} work in the template.   When it works, as
far as I can tell, in first pass, the_chair has a dictionary of all the
chair.0.* objects, second pass has a dictionary of all the chair.1.*
objects, etc.   I'm trying to figure out what to put into my custom
manipulator (RoomForm above) to make it work similarly.

 -Jonathan


--~--~-~--~~~---~--~~
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: Problem with custom forms and related objects

2007-01-02 Thread JMCameron


Thanks Justin!

I just figured this out myself.   I'll include the updated RoomForm
definition below since I've cleaned it up a bit and made a small fix to
the save function:

--

from django.db.models.related import RelatedObject

class RoomForm(forms.Manipulator):
   def __init__(self, num_chairs=3):
   self.fields = [
   forms.TextField(field_name='name', length=20, maxlength=30,
is_required=True),
   ]

   # Add extra fields for potential related chairs
   self.num_chairs = num_chairs
   for i in range(num_chairs):
   self.fields.append(
forms.HiddenField(field_name='chair.%d.id' % i, is_required=False) )
   self.fields.append(
forms.TextField(field_name='chair.%d.style' % i, length=20,
maxlength=30, is_required=False) )

   def get_related_objects(self):
   room_field = [x for x in Chair._meta.fields if x.name ==
'room'][0]
   return [ RelatedObject( Room, Chair, room_field ) ]

   def save(self, new_data):
   # Add the main Room object
   r = Room( name  = new_data['name'] )
   r.save()

   # Save any related chair objects containing data
   for i in range(self.num_chairs):
   if new_data['chair.%d.style' % i]:
   c = Chair( room = r, style = new_data['chair.%d.style'
% i] )
   c.save()
   return r

As you pointed out, the main trick is to define working
get_related_objects() function.

I'm looking forward to newforms.   Until it is ready, oldforms will do,
now that we've figured this little mystery out!

 -Jonathan


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