Note that there are some explanatory texts on larger screens.

plurals
  1. POEditing related models in profile form using django-profiles
    text
    copied!<p>I'm using <a href="http://bitbucket.org/ubernostrum/django-profiles/wiki/Home" rel="nofollow noreferrer">django-profiles</a> in my app, as it gives me a few simple views that helps me get where I want to go, faster.</p> <p>However, I have one problem. Given the models below, how can I create a form for editing a profile that includes all the fields on <code>UserProfile</code>, the first_name, last_name and email fields from <code>User</code>, and one or more <code>PhoneNumber</code>s?</p> <pre><code>from django.db import models from django.contrib.auth.models import User from django.utils.translation import ugettext_lazy as _ class UserProfile(models.Model): user = models.OneToOneField(User) height = models.IntegerField(_('Height'), max_length=3, blank=True, null=True, help_text=_('Users height in centimeters')) def get_absolute_url(self): return ('profiles_profile_detail', (), { 'username': self.user.username }) get_absolute_url = models.permalink(get_absolute_url) def __unicode__(self): return self.user.username class PhoneNumber(models.Model): description = models.CharField(_("Description"), max_length=32, blank=True) number = models.CharField(_("Phone number"), max_length=15) owner = models.ForeignKey(UserProfile, related_name="phone_numbers") def __unicode__(self): return u"%s (%s)" % (self.number, self.description) </code></pre> <p>The closest I've managed so far, is a form that includes all fields on <code>UserProfile</code>, and the wanted fields from <code>User</code>, using tricks explained <a href="http://birdhouse.org/blog/2009/06/27/django-profiles/" rel="nofollow noreferrer">here</a> and <a href="http://djangosnippets.org/snippets/2081/" rel="nofollow noreferrer">here</a>:</p> <pre><code>from django import forms from main.models import UserProfile from django.contrib.auth.models import User class UserForm(forms.ModelForm): class Meta: model = User fields = ('first_name', 'last_name', 'email') class ProfileForm(forms.ModelForm): def __init__(self, *args, **kwargs): # magic self.user = kwargs['instance'].user user_kwargs = kwargs.copy() user_kwargs['instance'] = self.user self.uf = UserForm(*args, **user_kwargs) # magic end super(ProfileForm, self).__init__(*args, **kwargs) self.fields.update(self.uf.fields) self.initial.update(self.uf.initial) def save(self, *args, **kwargs): # save both forms self.uf.save(*args, **kwargs) return super(ProfileForm, self).save(*args, **kwargs) class Meta: model = UserProfile exclude = ("user",) </code></pre> <p>In the admin, using Inlines on a custom ModelAdmin, I get the behaviour I want for the <code>PhoneNumber</code>s, but I've not been able to recreate it in a single form for use with django-profiles.</p> <p>Is it at all possible, or should I just ditch django-profiles and write my own profile-app that can use a formset to pull in the <code>PhoneNumber</code>s?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload