Can i create the same app multiple times in django? - python

I've created a fully functioning blog application in django . But i want to create multiple blogs like i can create multiple posts on blog. How to approach this problem?

No, you cannot have multiple Django apps of the same name. But, much like you’d have a Post model for a singular blog you can create a Blog table and have each Post have a foreign key to the Blog that post belongs to.

You can just have a one a app per django project beacuse we should import view.py and models.py and urls.py and ... in your some files !

Related

How can I modify models.py in external app in Django CMS

I am trying to modify existing model(In Django CMS Blog application). There is a Post class, I can modify it in models.py that located inside the Django CMS Blog project, like so:
media = PlaceholderField("media", related_name="media")
post_title = PlaceholderField("post_title", related_name="post_title") # My code
content = PlaceholderField("post_content", related_name="post_content")
liveblog = PlaceholderField("live_blog", related_name="live_blog")
And after the migration the DB looks like this.
As you can see, the field is added. But how can I do that from my local project files? I don't want to add this code inside 3d party app models, because it will lead to problems with updating this 3d party app.
I think the best way is to extend the Post model from Django CMS Blog in your app.
my_app/models.py
from djangocms_blog.models import Post
class MyCustomPost(Post):
# This will have every field in the parent Post model, plus the ones you add below.
my_custom_field = ...
Then use MyCustomPost (in your views, forms, admin,...) instead of using the original Post model.
Didn't find any solutions except to copy that app to your project folder and alter it there.

When to make a django app, rather than just a model

Recently I've been making a few test projects in Django and while I've found the structure to be better than that of other Web Frameworks, I am a little confused on the concept of different 'apps'.
Here is a test case example:
Suppose I have a simple CRUD application where users post a picture and a title, with a small description, but I want other users to have the ability to create a review of this picture.
Seeing as both the "Post" and "Review" models in this case require CRUD functionality, would I just have two models in the same app, and associate them with one another? Or have two separate apps with different urls.py and views.py files?
I have a hunch I've been doing it wrong and it should be just two models, if this is the case how would I go about writing the urls and views for two models in the same app?
Thanks and any input is appreciated!
The term application describes a Python package that provides some
set of features. Applications may be reused in various projects.
So in this case, better to have both Post and Review as two models in one app, since both of them apply to pictures.
how would I go about writing the urls and views for two models in the same app
The Django project provides a near perfect documentation on how to structure urls and views.
In your case, I think the better is to put your two models in one app.

Subclassing and overriding Django Class based views

I'm building a website using django-all auth for it's authentication and social authentication functions. The forms that come bundled with the app are hardly great to look at and hence I decided to create my own views.
The problem is: How do I create them while ensuring that the backend of Django all auth is still available to me? I've dug into the source code and found that it uses class based views for rendering and performing CRUD operations.
I want to know if I can subclass those views in my own app/views.py and just change their template_name field to my own templates. Any advice would be most helpful.
Thanks.
You can of course subclass the views, as long as you change your URLs to point to the overridden versions. However, there is no need to do this just to use your own templates; Django's template loader is specifically written with this use case in mind. Simply create your own directory inside your templates folder to match the one allauth is using, and create your own template files inside it; Django will find yours first and use them.

Django admin - Is there a way to have all object types in one form field?

The title might not be very clear. I'll try to explain be giving examples.
In my project, I have 6 different "content" apps. And we have several plugins we can put in pages (django-cms) having the possibility to have a link to any of thoses 6 apps.
But now, the only way I have for doing so, is to add a Foreign key for each apps, in each plugin.
Is there a better way ? Or is there a form field with the ability to link multiple different apps ?
I think what you need is generic relations in forms and admin - does this help?

Django Pinax , extending bundled applications

I want to use Pinax for a small project , but I am confused because I don't if can extend/change the behavior and functional of the provided applications .
Is there any documentation for extending the behavior of the bundled applications ?
example: in registration application ,I want to add custom fields but I am not able to find proper documentation on how to achieve it..( mainly for those which need db changes )
Thanks !
Yes, you can extend the behaviour of the built-in applications. If you are using the pinax basic setup with user accounts and profiles, you will have to add the extra fields you want in apps/profiles/models.py. For a list of field types, see here: https://docs.djangoproject.com/en/1.3/ref/models/fields/
This will create the necessary db fields for you when you run manage.py syncdb. If you have already sync'd the db, however, you will have to manually add the db columns. If you don't have any data you care about in that table, you can always just drop the table and it will recreate it. Django doesn't modify db tables once they are created, even if you change the model.
You will also have to modify the signup form to include these new fields and point your urls.py to the new signup form you created. Copy the form from the site-packages/pinax directory to your project. Don't modify them directly.
If you haven't already, you should check out the Django tutorial here: https://docs.djangoproject.com/en/1.3/intro/tutorial01/
This will give you a good idea of how Django apps are put together and how the different pieces interact, so you can do a better job customizing Pinax to your liking. Make sure you know what models.py, urls.py, views.py, and the templates are doing.

Categories