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.
Related
What is the best way to convert the project from django to django rest framework?
I have live django project, which is completely working, but now it want to convert the project into REST API.
I made the rest api for project which has new classes and function for the rest api with (form verification and same logic) copy from the django class which has been implemented.
So there is duplication of logic, so is there any way to do not replicate the logic and make running both of the classes django and django rest framework ?
Problem is when any update needed for any page then i have to code both side django and django rest framework , so there can be some mistake arise in copy.
One can create an API with Django using custom views but I advise one to use Django REST Framework (DRF) instead as it simplifies the process.
In short,
Install DRF with pip install djangorestframework (add it as well to the INSTALLED_APPS in your settings.py file).
Create a serializer per your needs (DRF has built-in serializers, like ModelSerializer). Note that OP can definitely use the models that OP already had in Django.
Create the views (DRF has generic views) and specify the URLs to access the views.
Let's say one has an app named books. In order to ensure the code isn't mixed between Django and the API, create an api folder inside of the books app and you should have something like this
books
-...
-api
--__init__.py
--serializers.py
--views.py
--urls.py
If OP wills, add reference to that urls.py in OPs main urls.py file,
urlpatterns = [
# ...
path('api/', include('books.api.urls', namespace='api')),
]
I have a django project with many applications in it, each one has models.py for pieces that are related to that portion of the project (The specific application) though I am using django's model login and user auth but I need more data with that, I saw here:
https://docs.djangoproject.com/en/2.2/topics/auth/customizing/#custom-permissions
That there is a way to create another model that has the extra information and make it a onetoone to the user.
The thing is, this extra data I need available to all my other applications, so what models.py do I stuff this new table to be built?
Is here a way to write a Django command to generate code automatically?
In my case: every time I create a new model I must create the following stuff too:
Create Administration classes in admin.py
Create service functions related to this model.
Create a factory using FactoryBoy.
Create test classes.
It would be nice if there was a command that generates this stuff automatically. Not everything, of course, but just the basic, the definition.
Is there something like this today in Django? Or is there a way I can write Django commands to generate code?
I have not personally used it yet but you could try to use the third-party package
Django baker Django Baker that offers that functionality
Django Baker wants to help you get your projects up and running
quickly. Given one or more app names, s/he will automatically generate
views, forms, urls, admin, and templates for all of the models in the
models.py file. All files are pep-8 compliant (with exception to the
maximum line length rule, which I don't agree with).
Once you add a single urlpattern to your project's URLconf, you'll
have a working list view, detail view, create view, update view, and
delete view for each model in your app.
Optionally you may specify which models in an app to bake if you'd
rather not generate files for all of them.
Try django commands and jinja2 templates.
With the execution of custom defined command, set of file templates can be updated with appropriate content and copied to respective folders as per need.
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 !
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.