Hi all, I'm just getting started with Django, so apologies in advance
for what has to be a complete n00b question.
I'm struggling to understand newforms. Specifically, when creating a
new record, I can't figure out how to populate the new record's parent
foreign key value. For example (leaving out imports and so forth):
---------------
models.py
---------------
class Foo(models.Model):
Id = models.IntegerField(primary_key=True, editable=False)
bar = models.ForeignKey('Bar', editable=False)
Foo_Text = models.CharField(blank=True, maxlength=150)
---------------
views.py
---------------
def add(request, bar_id):
FooForm= forms.models.form_for_model(Foo)
if request.method == 'POST':
form = FooForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/bar' + bar_id + '/')
else:
context = {'form': form, 'request': request, 'bar_id': bar_id,
'error_msg': "Please correct the error(s) below"}
else:
context = {'form': FooForm(), 'request': request, 'bar_id':
bar_id}
return render_to_response('foo.html', context)
In this case the user has already navigated to some bar record and is
trying to add a foo child record, so I know what the bar_id value is
already and I'm not exposing bar_id to the user as an editable field.
How do I get the current bar_id into foo record I'm creating with
form.save()?
This seems like it should be totally obvious, but I've been hammering
my head against it for hours with no result, so thanks very much for
any pointers.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---