3

I know this seems to be an over-asked question in the Django circles but I'm afraid to say that i've not yet found the solution to this.

My Model -

from djago.... import User
class InfoPersonal(models.Model):
...
person = models.OneToOneField(User)

I've tried overriding the save_model() in the admin definition and also overriding the save() in the Form but nothing seems to work.

If you were to auto add data into a ForeignKey or OneToOneField column to a Model how would you do it?

  def profile_creation_personal(request):
    if request.method == 'POST': # If the form has been submitted... 
        form = PersonalForm(request.POST) # A form bound to the POST data 
        # form.person = request.user
        if form.is_valid(): # All validation rules pass 
            # Process the data in form.cleaned_data 
            # ... 
            form.save()
            return HttpResponseRedirect('/done') # Redirect after POST
    else: 
            form = PersonalForm() # An unbound form 
    return render_to_response('info/personal/profile_create.html', { 'form': form,})


class PersonalForm(ModelForm):
    #hometown_id = ModelChoiceField(queryset=InfoReferenceCities.objects.all(),empty_label=None)
    def save(self, *args, **kwargs):
        self.person = request.user
        super(PersonalForm, self).save(*args, **kwargs)
    class Meta:
        model = InfoPersonal
        exclude = ('person',)
        widgets = {'dateofbirth' :  SelectDateWidget()} 
Sussagittikasusa
  • 2,525
  • 9
  • 33
  • 47
  • possible duplicate of [How to auto add data into a ForeignKey or OneToOneField column to a Model (DJANGO)](http://stackoverflow.com/questions/5281744/how-to-auto-add-data-into-a-foreignkey-or-onetoonefield-column-to-a-model-django) – Ignacio Vazquez-Abrams Mar 12 '11 at 10:30
  • Patience can kill a man, you know? – Sussagittikasusa Mar 12 '11 at 10:35
  • If you do programming the most important thing you need to learn is patience :) – Ski Mar 12 '11 at 11:00
  • Hahh but it's finally paid off man!, i feel good! I answered my own question ! nothing better than that, – Sussagittikasusa Mar 12 '11 at 11:02
  • Btw, this is my answer to this question: http://stackoverflow.com/questions/5033121/django-saving-multiple-modelforms-simultaneously-complex-case/5033242#5033242 – Ski Mar 12 '11 at 11:13

3 Answers3

1

I got the answer!!! I feel good!

personal = form.save(commit = False)
personal.person = request.user
personal.save()

This goes into the view much like Ignacio said, only commit = False being a critical statement for it to save an instance without throwing an exception. Thanks all who helped!!
Cheers

Sussagittikasusa
  • 2,525
  • 9
  • 33
  • 47
0

In your PersonalForm, you can subclass your save() function, so the appropriate data is added, something like this:

class PersonalForm(ModelForm):
    def save(self, *args, **kwargs):
        self.person = request.user
        super(PersonalForm, self).save(*args, **kwargs)

    class Meta:
        model = Personal
Bjorn
  • 5,272
  • 1
  • 24
  • 35
  • Except that you can't get there from here. (asker is in denial about the answer to the question that this is a duplicate of) – Ignacio Vazquez-Abrams Mar 12 '11 at 10:43
  • Hey Bjorn thanks a lot for replying, i think this might work only 'request' is termed as an undefined variable. what do i do about that? – Sussagittikasusa Mar 12 '11 at 10:48
  • @Ignacio - Hey i already tried form.person = request.user man, it didn't work...... – Sussagittikasusa Mar 12 '11 at 10:48
  • The logic should not be in the view imho. @Ignacio, you're right, but you can pass along the request object, it's explained here: http://stackoverflow.com/questions/631722/django-update-modelform – Bjorn Mar 12 '11 at 11:23
0

see this

parent = models.ForeignKey('self', blank=True, null=True, verbose_name=_("parent"))

this ok but have problem with sqlite , change it to postgresql its ok. ( its for my code , change it to your status )

Mohammad Efazati
  • 4,812
  • 2
  • 35
  • 50
  • Hey thanks for replying Efazati, but right now i can only use sqlite man, i think Bjorn's above might work, only got to figure a lil thing out. – Sussagittikasusa Mar 12 '11 at 10:54