I'm trying to create form, for adding new friend/updating existing one. My first solution uses ModelForm, but data from updated form are not saved. Also problem is that my model has CharFields ('code', 'pid'), that I need to validate with regex through my form. If I exclude those fields in Meta, and add them to the FriendForm, when the form is loaded I get no data there.
def add_friend(request):
userprofile = UserProfile.objects.get(user=request.user)
if request.method == 'POST':
form = FriendForm(request.POST, request.FILES,)
if form.is_valid():
form.save()
next = reverse('user_profile',)
return HttpResponseRedirect(next)
else:
form = FriendForm()
return render_to_response('user/data_operations/add_friend.html', {
'form':form, 'user':request.user,
}, context_instance=RequestContext(request))
def edit_friend(request, id):
userprofile = UserProfile.objects.get(user=request.user)
friend = get_object_or_404(Friend, id=id)
if friend.friend_of.id!=userprofile.id:
raise Http404
if request.method == 'POST':
form = FriendForm(request.POST, request.FILES, instance=friend)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('user_profile',))
else:
form = FriendForm(instance=friend)
return render_to_response('user/data_operations/edit_friend.html', {
'form': form, 'user': request.user, 'friend': friend,
}, context_instance=RequestContext(request))
class FriendForm(forms.ModelForm):
class Meta:
model = Friend
exclude = ( 'friend_of',)
The second solution uses forms.Form . Here the problem is that I cannot send instance to my form, so I cannot get data to edit. Because of that I'm not able to check if my save method works and if this solution is better.
def add_friend(request):
userprofile = UserProfile.objects.get(user=request.user)
count = 0
if request.method == 'POST':
form = FriendForm(request.POST, request.FILES,)
if form.is_valid():
form.save(user=userprofile)
next = reverse('user_profile',)
return HttpResponseRedirect(next)
else:
form = FriendForm()
return render_to_response('user/data_operations/add_friend.html', {
'form':form, 'user':request.user,
}, context_instance=RequestContext(request))
def edit_friend(request, id):
userprofile = UserProfile.objects.get(user=request.user)
friend = get_object_or_404(Friend, id=id)
if friend.friend_of.id!=userprofile.id:
raise Http404
if request.method == 'POST':
form = FriendForm(request.POST, request.FILES, instance=friend)
if form.is_valid():
form.save(user=userprofile)
return HttpResponseRedirect(reverse('user_profile',))
else:
form = FriendForm()
return render_to_response('user/data_operations/edit_friend.html', {
'form': form, 'user': request.user, 'friend': friend,
}, context_instance=RequestContext(request))
class FriendForm(forms.Form):
first_name = forms.CharField(widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=50)), label="First name")
last_name = forms.CharField(widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=50)), label="Last name")
pid = forms.RegexField(regex=r'^\d{11}', max_length=11 ,widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=50)))
image = forms.ImageField(label="Image", required=False)
street = forms.CharField(widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=50)), label="Street")
number = forms.CharField(widget=forms.TextInput, label="House/flat number")
code = forms.RegexField(regex=r'^\d{2}[-]\d{3}', max_length=6, widget=forms.TextInput(attrs=attrs_dict), label="Postal code")
city = forms.CharField(widget=forms.TextInput, label="City")
def save(self, user):
if self.is_valid():
from accounts.models import Friend
new_friend = Friend(**self.cleaned_data)
new_friend.friend_of = user
new_friend.save()
return new_friend
Here are my models used :
class UserProfile(models.Model):
(...)
user = models.ForeignKey(User, unique=True, related_name='profile')
class Friend(models.Model):
(...)
friend_of = models.ForeignKey(UserProfile, related_name='friend_of')
How can I get this working ? My form for editing user (using model method) works without any problems :/