#7006: Overridden fields/widgets for ForeignKeys in ModelForms
----------------------------------------------------------+-----------------
          Reporter:  Simon Litchfield <[EMAIL PROTECTED]>  |         Owner:  
msaelices
            Status:  assigned                             |     Milestone:  1.0 
     
         Component:  Forms                                |       Version:  SVN 
     
        Resolution:                                       |      Keywords:      
     
             Stage:  Accepted                             |     Has_patch:  0   
     
        Needs_docs:  0                                    |   Needs_tests:  0   
     
Needs_better_patch:  0                                    |  
----------------------------------------------------------+-----------------
Comment (by msaelices):

 I've tested ticket with this models and form (used admin site for test):
 {{{
 #!python

 # Models
 class Affiliate(models.Model):
     name = models.CharField(max_length=100)

 class Booking(models.Model):
     first_name = models.CharField(max_length=100)
     last_name = models.CharField(max_length=100)
     affiliate = models.ForeignKey(Affiliate, null=True, blank=True,
 related_name='bookings')

 # Forms
 class BookingForm(forms.ModelForm):
     affiliate = forms.CharField(max_length=20, required=False,
 label='Affiliate Code')

     class Meta:
         model = Booking
         fields = ('first_name', 'last_name', 'affiliate')

 # Admin
 class BookingAdmin(admin.ModelAdmin):
     form = BookingForm


 admin.site.register(Affiliate)
 admin.site.register(Booking, BookingAdmin)
 }}}

 Admin rendered successfully a {{{CharField}}}, but problem was adding a
 {{{Booking}}} object.

 The traceback I got when tried to adding a {{{Booking}}} object with an
 affiliate value of {{{'1'}}} was:
 {{{
 Traceback:
 File "/var/lib/python-support/python2.5/django/core/handlers/base.py" in
 get_response
   86.                 response = callback(request, *callback_args,
 **callback_kwargs)
 File "/var/lib/python-support/python2.5/django/contrib/admin/sites.py" in
 root
   173.                 return self.model_page(request, *url.split('/', 2))
 File "/var/lib/python-support/python2.5/django/views/decorators/cache.py"
 in _wrapped_view_func
   44.         response = view_func(request, *args, **kwargs)
 File "/var/lib/python-support/python2.5/django/contrib/admin/sites.py" in
 model_page
   192.         return admin_obj(request, rest_of_url)
 File "/var/lib/python-support/python2.5/django/contrib/admin/options.py"
 in __call__
   185.             return self.add_view(request)
 File "/var/lib/python-support/python2.5/django/db/transaction.py" in
 _commit_on_success
   238.                 res = func(*args, **kw)
 File "/var/lib/python-support/python2.5/django/contrib/admin/options.py"
 in add_view
   483.                 new_object = self.save_form(request, form,
 change=False)
 File "/var/lib/python-support/python2.5/django/contrib/admin/options.py"
 in save_form
   361.         return form.save(commit=False)
 File "/var/lib/python-support/python2.5/django/forms/models.py" in save
   218.         return save_instance(self, self.instance,
 self._meta.fields, fail_message, commit)
 File "/var/lib/python-support/python2.5/django/forms/models.py" in
 save_instance
   43.         f.save_form_data(instance, cleaned_data[f.name])
 File "/var/lib/python-
 support/python2.5/django/db/models/fields/__init__.py" in save_form_data
   406.         setattr(instance, self.name, data)
 File "/var/lib/python-
 support/python2.5/django/db/models/fields/related.py" in __set__
   264.                                  self.field.name,
 self.field.rel.to._meta.object_name))

 Exception Type: ValueError at /admin/ticket7006/booking/add/
 Exception Value: Cannot assign "u'1'": "Booking.affiliate" must be a
 "Affiliate" instance.
 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/7006#comment:8>
Django Code <http://code.djangoproject.com/>
The web framework for perfectionists with deadlines
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to