model functions to interact with other models

25 views
Skip to first unread message

Brandon Graves

unread,
Apr 13, 2017, 1:19:35 PM4/13/17
to Django users
I've been trying to find some "best practice" information on this and have been coming up dry.

I have a "User" Model and an "event" model that tracks things that happens to users: Start date, End date, Status, etc.
From a purely logical Object oriented perspective, it feels right to add functions to the User model that will handle getting/updating/saving/changing Events.

so that User.GetStart() could be called, or User.ChangeStatus("Active") or something, would find the current non-superseded event relevent to the EventType, and supersede it, and save a new event... Is this ok? is that a bad idea? In my head it seems like the most natural and logical way to do it, but my gut is telling me that I should seek some advice before I go down this path.

Any input would be appreciated!

Antonis Christofides

unread,
Apr 14, 2017, 11:44:52 AM4/14/17
to django...@googlegroups.com

First, common practice is to name methods with lower case and underscores. Now, you probably don't need User.get_start(), because if "user" is a "User" you can usually type in something like "user.event_set.latest()". If the query is more complicated and you use it many times, I think it's OK to create user.get_start() or something.

I don't have anything against User.change_status(); I think it would work. However if there's no compelling reason I'd probably do it explicitly rather than call a method:

event = Event(user=user, ...)
event.save()
Antonis Christofides
http://djangodeployment.com
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6a7af583-6de4-457f-af57-62fc41185a9c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages