Hello django-users,
I am experimenting a bit with the newforms library.
My problem is, that I use a Foreign-Key in a Table and want to view
(and edit) only one entry of this Table.
The request.POST gives no user-value and the form is not valid,
because of this missing value.
How can I add the foreignkey (user-id) to the form? Using a hidden-field? How?
Thanks for your help.
The Modell:
class UserProfile(models.Model):
GENDER_CHOICES = (
('M', 'Male'),
('F', 'Female'),
)
gender = models.CharField(max_length=1, choices=GENDER_CHOICES, blank=True)
user = models.ForeignKey(User, unique=True, verbose_name=_('user'))
birthday = models.DateField(null=True, blank=True)
The View:
def edit_profile(request, uid=None):
if uid:
auth_user = User.object.get(username=uid)
elif request.user:
auth_user = request.user
# TODO else missing!!
user_id = auth_user.id
if request.method == 'POST':
UserForm = forms.form_for_model(UserProfile)
form = UserForm(request.POST)
if form.is_valid():
form.save()
# Do form processing here...
return HttpResponseRedirect('/')
else:
UserObject = get_object_or_404(UserProfile, user=user_id)
UserForm = forms.form_for_instance(UserObject,
fields=('gender', 'birthday'))
form = UserForm()
return render_to_response('edit.html', {'form': form})
--
Andreas Madsack
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" 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-users?hl=en
-~----------~----~----~----~------~----~------~--~---