Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Reusable Django Apps?

I am somewhat of a Django beginner and have been trying to decouple my applications as much as possible and build it in as small re-usable pieces as possible. Trying to follow James Bennett's strategy of building re-usable apps. With that in mind, I came across this problem.

Let's say I had an app that stores information about movies:

The code would look something like this:

class Movie(models.Model):
    name = models.CharField(max_length=255)
    ...

Now, if I wanted to add ratings, I could use django-rating and simply add a field to my model:

class Movie(models.Model):
    name = models.CharField(max_length=255)
    rating = RatingField(range=5)
    ...

This inherently mean that my Movie app is now dependent on django-ratings and if I wanted to re-use it, but no longer needed ratings, I would still have to install django-ratings or modify and fork off my app.

Now, I could get around this by use try/except with import and define the field if successful, but now my movie app is explicitly tied to the rating in the database table definition.

It seems much more sensible to separate the two models and define the relationship in the ratings model instead of the Movie. That way the dependency is defined when I use the rating, but not needed when using the Movie app.

How do you deal with this problem? Is there a better approach to separate the models?

I also wonder if there are any major performance penalties in doing this.

edit: I want to clarify that this is more of an example of the problem and a somewhat contrived one at that to illustrate a point. I want to be able to add additional information without modifying the "Movie" model every time I need to add related data. I appreciate the responses so far.

like image 473
Bruce Lee Avatar asked May 11 '11 03:05

Bruce Lee


People also ask

Can you make apps with Django?

Yes. Use the standard Android app tools and use Django to serve and process data through API requests.

How many apps can a Django project have?

Django comes with six built-in apps that we can examine.

How do I organize my Django apps?

The way I like to organize my Django Project is – Keeps all Django apps in apps folder, static files (scripts, js, CSS) in the static folder, HTML files in templates folder and images and media content in the media folder.


1 Answers

In this case, personally, I'd keep it simple and just leave rating on the model. You have to balance re-usability with simplicity of implementation. It's great to make things reusable, but is your Movie model really useful enough to warrant the extra work? And is it that bad to have a dependency? I think much of these design decisions are subjective. There was a good talk this year at PyCon on this subject: http://blip.tv/file/4882961

like image 109
zeekay Avatar answered Oct 19 '22 02:10

zeekay