Re: newforms pre-populating with model

2008-02-24 Thread James Bennett

On Sun, Feb 24, 2008 at 11:02 PM, k4ml <[EMAIL PROTECTED]> wrote:
>  Get the instance:-
>  customer = Customer.objects.get(id=1)
>
>  Create form instance and pass a dict of data from customer instance:-
>  form = CustomerForm(initial=customer.__dict__) #instance of ModelForm

If you have a look at the ModelForm documentation, you'll see that its
constructor takes a keyword argument 'instance', which is an existing
object to populate initial data; under the hood, it uses a much more
robust method of fetching the data out of the model instance
(django.newforms.models.model_to_dict() if you're interested).


-- 
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

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



newforms pre-populating with model

2008-02-24 Thread k4ml

I guess my use case is very common but somehow I can't find any
examples that did as what I want. I just want to provide a form to
edit an existing record so it supposed to be as simple as:-

Get the instance:-
customer = Customer.objects.get(id=1)

Create form instance and pass a dict of data from customer instance:-
form = CustomerForm(initial=customer.__dict__) #instance of ModelForm

The problem is, if my models has a foreign key fields, the rendered
select input would not set the initial value (selected) because the
select input name="state" while the key in customer.__dict__ is
state_id.

The models:
class Customer(models.Model):
   state = models.ForeignKey(State, db_column='state',
verbose_name='Negeri')

and the form:-

class CustomerForm(forms.ModelForm):
   class Meta:
   model = Customer

>>> customer.__dict__
{'created': None, 'created_new': None, 'notes': u'Test', 'refferer':
1, 'state_id': u'kelantan', 'id': 2}

While customer.state is an instance of State object and does not
appear in .__dict__ and even it appear newforms won't render. The
solutions that I can think of:-

1) Use state_id as a db_column - Don't really like it since 'state'
better expressed the data it contain.
2) Explicitly set every form field - Don't like it either since it
some kind of violate DRY. The fields already explicitly defined in the
models so we should assumed that as an authoritative definition. We
just feed it with a dict and let the validation routine handle the
rest.

I'd really hope someone can point me to the right direction, maybe I'm
missing something somewhere ? 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
-~--~~~~--~~--~--~---