Implementing a List of Foreign Keys in a Model

1,155 views
Skip to first unread message

Kurtis

unread,
Sep 10, 2011, 7:18:28 PM9/10/11
to Django users
Hey Guys,

I have a very simple stub of a project. I'm trying to do something
that should be very simple but isn't as easy as I hoped.

I have a UserProfile class. This object is basically just an extension
of the standard User Class. Within my UserProfile class, I want to
have a list of PublicProfile foreign keys. PublicProfile itself is an
abstract class with multiple classes extending it. For example, a User
can create an ArtistProfile, a BandProfile, etc...

To give you a sense of what I'm trying to accomplish from a user's
perspective: A user can sign up and create multiple Pages (represented
as PublicProfile) for bands, artists, etc...

Here's some simple code I started with but I have no idea how to
proceed:

###########################################################

class UserProfile(models.Model):

# The Associated User
user = models.OneToOneField(User)

# The User's Profiles
# What do I do here? I don't know how to start out with an empty
list and add
# ForeignKeys to it as I go.
# profiles = ...?

class PublicProfile(models.Model):

username = models.CharField(max_length = 40)
zipcode = models.CharField(max_length = 10)
# ....

class Meta:
abstract = True

class ArtistProfile(PublicProfile):
pass

class BandProfile(PublicProfile):
pass

###########################################################

Thanks!

Daniel Roseman

unread,
Sep 11, 2011, 4:32:36 AM9/11/11
to django...@googlegroups.com

On Sunday, 11 September 2011 00:18:28 UTC+1, Kurtis wrote:
Hey Guys,

I have a very simple stub of a project. I'm trying to do something
that should be very simple but isn't as easy as I hoped.

I have a UserProfile class. This object is basically just an extension
of the standard User Class. Within my UserProfile class, I want to
have a list of PublicProfile foreign keys. PublicProfile itself is an
abstract class with multiple classes extending it. For example, a User
can create an ArtistProfile, a BandProfile, etc...

To give you a sense of what I'm trying to accomplish from a user's
perspective: A user can sign up and create multiple Pages (represented
as PublicProfile) for bands, artists, etc...



--
DR. 

Kurtis Mullins

unread,
Sep 12, 2011, 10:44:46 AM9/12/11
to django...@googlegroups.com
Thanks a lot for that tip. I actually read over the Generic Foreign Keys several times before I asked the question but it still left me pretty confused. Is there a place I can see a good, straight-forward example of how this works? The "tags" example they had just didn't do it for me. Plus, they didn't really show how to call it from a command line which would be important to me.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/MbkMEt65QFcJ.

To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply all
Reply to author
Forward
0 new messages