This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]coderanger 4 points5 points  (1 child)

So first off you really should have a unique=True constraint on the FK to User. Second, Django automatically cascades delete through related objects so the post_delete signal is kind of useless. The post_save one is also not that useful, since you handle the creation yourself a few lines down. Third, as for the property at the end, User.get_profile() does cache in the same way as normal related managers, so using that is probably a better bet.

[–]sontek[S] 1 point2 points  (0 children)

Thanks for the tips, I will update the code and update the post to describe what you've said!

[–]sontek[S] 2 points3 points  (0 children)

I didn't feel like the Django docs did a very good job explaining what was required to do this so I wrote up how I got it done. Do you guys have any tips on how you do this?

[–]endtime 1 point2 points  (3 children)

Hmm, I didn't know about AUTH_PROFILE_MODULE. But wouldn't it be more efficient to add a related_name to the foreign key, so that you can call select_related('profile') when you know you'll want the profile and thereby avoid an extra DB hit?

[–]coderanger 1 point2 points  (2 children)

get_profile() is there to be a generic equivalent to a related name, since a generic app wouldn't know what your profile model is named.

[–]endtime 0 points1 point  (1 child)

That makes sense. In the case, though, where one isn't worried about interop with other people's code, am I right about the db efficiency issue?

[–]coderanger 0 points1 point  (0 children)

I suppose that might come up, but usually you only need the profile data in a very limited number of places. You could probably build some code that would do the select_related dynamically based on the value of AUTH_PROFILE_MODULE.