How call this backend coding? - python

Lets say I've following table:
Table: Posts
Fields: id, name, content
And another table:
Table: Images
Fields: id, post_id, url
Normally, I can create CRUD for each of them. User first create post and save it. Then by Images table CRUD user can add as many as images to posts. But what if requirement is no create CRUD for images. But inside Posts CRUD. I mean while creating (filling Posts fields) there is button which labeled "add images". Then when user add images via that button. Finally when press on "create post" backend code should create both posts and images. How you call it? I couldn't find any tutorial and lesson because of my English I couldn't describe it enough on Google search.

You can use inlines to implement what you want in django admin:
from django.contrib import admin
class ImagesInline(admin.TabularInline):
model = Images
class PostsAdmin(admin.ModelAdmin):
inlines = [
BookInline,
]
admin.site.register(Posts, PostsAdmin)

Related

Access Django DB objects without shell?

I have a request - can you help me access and manage django DB objects without using shell ?
I have created my models, but now (for example) i want to make a login system. I store users and passes(again, only an example), and i want to get the info from the DB, but i dont want to use shell.
What can i do in this case, im quite new to Django ?!
Best Regards
Why not use django-admin?
Maybe this is what you want:https://docs.djangoproject.com/en/3.0/ref/contrib/admin/
In views.py you can import
from .models import modelname
data = modelname.objects.all() - using this you can get all the data from the Database
Eg:-
for d in data:
print (d.email)
Will give all emails in the database
You can also use
t = modelname.objects.get(email='name#lk.com')
By this you can get the data of the person who's email is name#lk.com
Django already has database support where you can register your models and access them with a graphical interface.
See the documentation: django-admin-site
First you need to create a super user account, if you don't have one, create it with the terminal in the project directory, use this row:
python manage.py createsuperuser
For your model to appear on the admin site you need to register it
# models.py
class MyModel(models.Model)
field1 = models.CharField()
field2 = models.TextField()
# ...
# admin.py
from django.contrib import admin
from .models import MyModel
admin.site.register(MyModel)
So it's the basic way to register your model, if you want to personalize you need to check the documentation ModelAdmin.fieldsets
with this done, just access the admin site at the link http://localhost:8000/admin/ and log in with the super user account and you will see the model registered.

I want to add costume field after django social app login

Hi I am php developer new to python/django
I'm creating a social login with django using 'social-auth-app-django' library and i followed following tutorial to implement it.
https://simpleisbetterthancomplex.com/tutorial/2016/10/24/how-to-add-social-login-to-django.html
Its working fine but i also need to add costume files in database which will be in different table but it will be get added when new user is created.
I have extended the user table as following
from django.contrib.auth.models import User
class NewsCreator(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
CreatorLastLogs= models.CharField(max_length=100)
CreatorLogs= models.CharField(max_length=100)
and i want to add data to these fields when a new user is created or when existing user logins. I tried going through documentation but could not found any thing that is related to code extension/customisation etc. Thanks in advance
Hi i have found answer to this so i'm posting for people who will stumble upon this post later.
django social provides pipeline to extend their code, and we just have to extend this pipeline
for this in your setting.py file post following list(all in this list are default pipeline methods which gets called except for last one).
SOCIAL_AUTH_PIPELINE = (
'social_core.pipeline.social_auth.social_details',
'social_core.pipeline.social_auth.social_uid',
'social_core.pipeline.social_auth.auth_allowed',
'social_core.pipeline.social_auth.social_user',
'social_core.pipeline.user.get_username',
'social_core.pipeline.user.create_user',
'social_core.pipeline.social_auth.associate_user',
'social_core.pipeline.social_auth.load_extra_data',
'social_core.pipeline.user.user_details',
'newsapp.pipeline.save_profile'<-- this is your method
)
create a file in your app with name pipeline.py and name of method is to be provided in list above like last string in list (newsapp is name of my app provide your appname)
in your pipeline.py file
def save_profile(backend, user, response, *args, **kwargs):
if NewsCreator.objects.filter(user_id=user.id).count() == 0 :
newsCreator = NewsCreator.objects.create(user=user)
//your logic for new fields
newsCreator.save()
if you have any other query regarding django-social you can refer
https://github.com/python-social-auth/social-docs
its detail documentation

Override Flask Admin save new row in table

How would I be able to override and add some extra code when creating a new row in a table via the Admin panel in Flask?
For example: User enters info for a new row in the 'Post' table and clicks save. I want to add some code to automate a process with that new row information.
You can override the methods on_model_change to perform actions before saving/updating a new model, or after_model_change to do something after, obviously.
You can inherit from the class BaseModelView or ModelView if you are using Flask-SqlAchemy.
In every cases, 3 arguments are provided to play with : the form used by the view, the new/updated model and the flag is_created to know if the model is new (True) or updated.
You can defined the model view like below :
from flask_admin import Admin
from flask_admin.contrib.sqla import ModelView
class PostView(ModelView):
def after_model_change(self, form, model, is_created):
print(form, model, is_created)
def on_model_change(self, form, model, is_created):
print(form, model, is_created)
admin = Admin(template_mode='bootstrap3')
admin.add_view(PostView(Post, db.session, name='Posts'))
Note : You have access to similar methods for the deleting part with on_model_delete and after_model_delete, except you only have the model given as argument.

How to extend an admin view in Django?

I would like to extend the admin view used to create a new entry in the database.
For example I can create new books with their author, their categories, their date etc...
When I create a new book I want to send a notification to all users whose the book's author is one of their favorite authors.
The notification system is already working thanks to django-notifications
So, I just have to add this kind of stuff just after the book.save() in the admin view :
writer = <the_author_selected_in_the_form> # writer ID
profiles = Profile.favorites_authors.through.objects.filter(authors_id=writer)
for profile in profiles:
notify.send(profile.profile_id, recipient=profile.profile_id, verb='New book, called <book_title>, from <author_name>')
Thank you
I think "the Django way" to do that is by using signals. Something like this:
from my.book.app import Book
from django.db.models.signals import post_save
def book_created(sender, instance, **kwargs):
# Notification code goes here
if kwargs["created"]:
notify_users()
post_save.connect(book_created, sender=Book)
You can read more about them in the Django docs, also there are some great articles for beginners.

django admin forms. how to skip validation?

I have some form:
class Node(Object):
title = CharField(max_length=255)
body = TextField()
in admin form there is file field (which is a dump,for example csv dump of Nodes)
and what i want to do:
if user fills "data_file" i want to skip all form validation, and create Nodes from data_file content. Otherwise i'll create Node based on "title" and "body fields"
I need help because can not find anything about my problem in django docs
There is method to create a url view, but i want to do it by django standart admin tools

Categories

Resources