Hi Giuseppe, On 2/17/07, Giuseppe Franchi <[EMAIL PROTECTED]> wrote: > > Hello everyone! > I've approached Django and Python from a few time and i love them > both. > As i am newbie i have a noobish question. My problem is: i have to > store a Resource object splitted in two tables as following: > > class Resource(models.Model): > > title = models.CharField(maxlength=100) > creator = models.CharField(maxlength=50) > ...blah blah > > class Resource_systeminfo(models.Model): > > resource_ID = models.ForeignKey(Resource) > owner = models.ForeignKey(User) > date_birth = models.DateField() > > > (that represent who added the resource in the db, and when) > All the info in the Resource table are filled in by a user. As a user > insert a new resource, i wish to create in automatic a > Resource_systeminfo instance linked to the first, but i have no idea > about how to do that... > Actually my view is > > @login_required > def resource_edit_insert(request, id=None): > ResourceForm = form_for_model(Resource) > if id is not None: > pass > #...i still have to implement resource modification :) > else: > if request.POST: > form = ResourceForm(request.POST) > if form.is_valid(): > form.save() > '''Here starts my problem. I tried to use a form_for_model (great > tool) but obviously i have also to insert the foreign key to the > Resource object... and i absolutely dunno how to do it!'''
you can do mod = form.save( commit=False ) # now edit any fields of mod you wish mod.save() # manually call the save > ResourceSystemForm = > form_for_model(Resource_systeminfo) > data = {'owner': request.user, 'date_birth': > datetime.date.today()} > systemform = ResourceSystemForm(data) > systemform.save() > return HttpResponseRedirect('/mainpage/') > else: > context = {'form': form, 'error_message': "Please > correct errors below."} > else: > context = {'form': ResourceForm()} > return render_to_response( > 'catalog/resource_edit_insert.html', > context, > context_instance=RequestContext(request) > ) > > > If someone knows how it could be done... A note: I would prefer not to > join the two tables, keep it as very last solution! > > Cheers and sorry for my terrible english. > > > > > -- Honza Kr�l E-Mail: [EMAIL PROTECTED] ICQ#: 107471613 Phone: +420 606 678585 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---