tags:

views:

40

answers:

2

Can I give the same name for different Models in different apps? and What conflicts can happen?

After I have a try.. I got this Error:

Error: One or more models did not validate:
playlist.playlist: Accessor for field 'user' clashes with related field 'User.playlist_set'. Add a related_name argument to the definition for 'user'.
audio_playlist.playlist: Accessor for field 'user' clashes with related field 'User.playlist_set'. Add a related_name argument to the definition for 'user'.
+4  A: 

Of course you can do that. There won't be any conflicts because tables are stored internally as appname_modelname; let's say you have a model named Post in an app named blog and a model named Post in an app named messages. Tables will be stored as blog_post and messages_post. Also, the python classes are named project.blog.models.Post and project.messages.models.Post, so no conflict here either.

EDIT: Also, to be able to import them both in one file, use something like this:

import blog.models.Post as BlogPost
import messages.models.Post as MessagesPost

(or any names that make sense)

Gabi Purcaru
And what gonna happen if you load both model in the one view? Or if you do command shell_plus for example?
Pol
make sure the names won't collide. by that I mean something like `import blog.models.Post as BlogPost` and `import message.models.Post as MessagesPost`
Gabi Purcaru
What about shell_plus it load models as they are... Does it give me an error?
Pol
it won't give you an error; the worst thing that can happen is rewriting the Post object. Other than that, it behaves just like the script.
Gabi Purcaru
Do you suggest to not do that?, or its dos not matter?
Pol
@Pol if you need two objects with the same name it's normal to hold them with two different names.
Gabi Purcaru
A: 

Django is built on Python. Each app has it's own namespace.

MovieYoda