aemdy
aemdy

Reputation: 3802

Django view, models, form, app naming

I have a question: "How should I name apps, views, models, forms, fields, etc?"

For example I have a browser game that has a mailbox implemented.

App cannot be named mailbox so I name it pm_box (is that good?)

Then I have to create model where all the messages are held. What model name should be? Message or Messages? It should have a boolean field which is True if message has already been read. Should that field be called read or is_read?

Then I have a view that lists the messages. Should I name it list_messages, message_list, message_list_view or list_messages_view (if I do not specify inbox/outbox)?

The the form for new message input data validation. Should the form be named NewMessageForm, MessageWritingForm..?

After that I want to keep track of timestamps for every player. For this purpose I have Player model (or should it be Players)? With OneToOne field to the user and OneToOne field to Timestamp model (or timestamps) that has fields: online, last_pm_sent, some_action.

Thanks for all your answers. I have already read django styling documentation and pep, however, nowhere these things are specified.

EDIT: The project name (in pyCharm) is my game name. How should I name the first app and where should I hold the Player(s) model (in which app) which is UserProfile as well.

Upvotes: 6

Views: 4252

Answers (1)

Usman Chaudhry
Usman Chaudhry

Reputation: 430

Alright starting with what you've named, it must not be pm_box, try naming discretely, if something is keyword try finding an appropriate synonym, here PersonalMessage and your view folder will be personal_messages while file will be usually good like single word, eg. enlist.html, display.html, etc. will be good, we mostly follow this structure in django:

Model Class Name

Singular, If mutliple used upper letter camel casing -> Example: Person, User, Subject, StudentSubject, StudentGuardian etc.

Form Class Name:

Relevant Model Class Followed by Form -> Example: PersonForm, UserForm, SubjectForm etc.

Boolean Variables:

Proper with is_ or has_ prefix -> Example: is_present, is_available, is_online, has_parent, etc.

Views:

Put in folder plural of model, but instead of upper letter use underscore(_) after every word (in case of two letters) file name will be like function name -> Example: people/index.html, people/detail.html, student_guardians/display_fee.html etc.

Though you may not entirely need a new model to keep 1-to-1 relationship, a better approach would be to add more fields to current table. Timestamp conventions could be like, last_visited_at, last_played_at, etc.

You could give app a name by defining in model as:

class UserProfile(models.Model):
    pass
    class Meta:
        app_label = 'Your Application Name'

Upvotes: 18

Related Questions