Sunday, April 7, 2013

Re: How do I have company display in admin drop down box?

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.19 (GNU/Linux)

iEYEARECAAYFAlFiElMACgkQ12ptzpkpIltZfwCfRSTrdRwmh6w0R6uXBYffG7I9
RckAoIHewqCTVMgxqBGKU5CL+q3tiCYt
=KzV6
-----END PGP SIGNATURE-----
On Sun, 7 Apr 2013 17:01:52 -0700 (PDT)
frocco <farocco@gmail.com> wrote:

> I have a model that references User and on the admin page, it shows the
> user name.
> I have the User tied to a UserProfile model that has a field called
> company.
>
> I want the drop down on my Pricing Model to show company, not user.
>
> user = models.ForeignKey(User)
>

Possibly something like this in your forms.py

#forms.py

class PricingForm(forms.ModelForm):
class Meta:
model = PricingModel
def __init__(self, *args, **kwargs):
super(PricingForm, self).__init__(*args, **kwargs)
self.fields['user'].choices =
((c.id,c.company) for c in UserProfile.objects.all())

or specify the choice in your model like this

user = models.ForeignKey(
User,
help_text='Select the user to apply this pricing',
choices=((c.id,c.company) for c in UserProfile.objects.all())
)

--
Drew

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate