Comments (11)
The CreateAPIView
and ListCreateAPIView
extends the CreateModelMixin
that allow to perform creare, just save
class CreateModelMixin:
"""
Create a model instance.
"""
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
def perform_create(self, serializer):
serializer.save()
see the CreateAPIView
-> docs
So, CreateModelMixin
in create
function call the perform_create
, and just call the save method of the serializer, if you override the save method, and maybe use a bulk option like bulk_create
or bulk_update
, not trigger the signals
from django-rest-framework.
Thank you. @FraCata00 . I calling post_Save()
with it
def perform_create(self, serializer):
instance = super().perform_create(serializer)
post_save.send(sender=type(instance), instance=instance, created=True)
return instance
I'm just wondering if there isn't a more appropriate way to trigger this other than customizing the view and expanding CreateAPIView?
from django-rest-framework.
Thank you. @FraCata00 . I calling
post_Save()
with itdef perform_create(self, serializer): instance = super().perform_create(serializer) post_save.send(sender=type(instance), instance=instance, created=True) return instance
I'm just wondering if there isn't a more appropriate way to trigger this other than customizing the view and expanding CreateAPIView?
But have you registered the signals in the ready function of AppConfig?
If you did, there's no need to call it in perform_create, trigger automatically
from django-rest-framework.
Thank you. @FraCata00 . I calling
post_Save()
with itdef perform_create(self, serializer): instance = super().perform_create(serializer) post_save.send(sender=type(instance), instance=instance, created=True) return instance
I'm just wondering if there isn't a more appropriate way to trigger this other than customizing the view and expanding CreateAPIView?
But have you registered the signals in the ready function of AppConfig?
Yup, I have done the registration in app.py and this code works fine. I just don't think it's clean enough.
from django-rest-framework.
Yeah, the best practice to call the signals, is write a custom signals like post_save
or ever you want, just register it in AppConfig ready
function inside the <application_module>.apps.py
Ever the instance call the .save
method, the signals (if receiver is pre_save
or post_save
) is triggered
There's no sense write custom perform_create
only to trigger the signals
Can you put your apps.py
and the signals?
from django-rest-framework.
Yeah, the best practice to call the signals, is write a custom signals like
post_save
or ever you want, just register it in AppConfigready
function inside the<application_module>.apps.py
Ever the instance call the
.save
method, the signals (if receiver ispre_save
orpost_save
) is triggered There's no sense write customperform_create
only to trigger the signalsCan you put your
apps.py
and the signals?
Yeah, there are mine apps.py
and signals config:
apps.py
from django.apps import AppConfig
class SettingsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'settings'
def ready(self):
import settings.signals
singals.py
@receiver(post_save, sender=Menu)
def sync_menu(sender, instance, created, **kwargs):
if created:
print('sync menu')
sync_menu_to_front.delay(instance.id)
from django-rest-framework.
this signals is from a project module or an application module?
from django.apps import AppConfig
class PortfolioConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'settings'
def ready(self):
import settings.signals # --> the 'settings' is an application or project?
Yeah, the best practice to call the signals, is write a custom signals like
post_save
or ever you want, just register it in AppConfigready
function inside the<application_module>.apps.py
Ever the instance call the.save
method, the signals (if receiver ispre_save
orpost_save
) is triggered There's no sense write customperform_create
only to trigger the signals
Can you put yourapps.py
and the signals?Yeah, there are mine
apps.py
and signals config: apps.pyfrom django.apps import AppConfig class PortfolioConfig(AppConfig): default_auto_field = 'django.db.models.BigAutoField' name = 'settings' def ready(self): import settings.signals
singals.py
@receiver(post_save, sender=Menu) def sync_menu(sender, instance, created, **kwargs): if created: print('sync menu') sync_menu_to_front.delay(instance.id)
from django-rest-framework.
this signals is from a project module or an application module?
from django.apps import AppConfig class PortfolioConfig(AppConfig): default_auto_field = 'django.db.models.BigAutoField' name = 'settings' def ready(self): import settings.signals # --> the 'settings' is an application or project?Yeah, the best practice to call the signals, is write a custom signals like
post_save
or ever you want, just register it in AppConfigready
function inside the<application_module>.apps.py
Ever the instance call the.save
method, the signals (if receiver ispre_save
orpost_save
) is triggered There's no sense write customperform_create
only to trigger the signals
Can you put yourapps.py
and the signals?Yeah, there are mine
apps.py
and signals config: apps.pyfrom django.apps import AppConfig class PortfolioConfig(AppConfig): default_auto_field = 'django.db.models.BigAutoField' name = 'settings' def ready(self): import settings.signals
singals.py
@receiver(post_save, sender=Menu) def sync_menu(sender, instance, created, **kwargs): if created: print('sync menu') sync_menu_to_front.delay(instance.id)
I'm sorry I'm in a drunken state.
It's from application module, I changed I pasted the code for the settings module.
from django-rest-framework.
However, the signals works fine right? If you create manually (maybe from django shell) a Menu object instance, the signals trigger correctly?
from django-rest-framework.
Yup, It's working for now.
It's just that for my case, I need to extend the perform_update method for every similar API for every application, and depending on the permissions and the functionality of the API, multiple APIs need to be customized. I'm guessing that scenarios with needs like mine should be relatively common. Is there a possibility to be able to add a method to perform_create
from django.db.models.signals import post_save
def perform_create(self, serializer):
instance = super().perform_create(serializer)
if(post_save):
post_save.send(sender=type(instance), instance=instance, created=True)
return instance
from django-rest-framework.
So, this isn't a best practice 😞
The perform_create, just call the .save
method of the serializer and after that:
- call the
.save
method of the model instance - trigger the signals --> in your case the
sync_menu
post_save
Can you put here the serializer code?
from django-rest-framework.
Related Issues (20)
- 3.15 regression: ListSerializer ValidationError nested structure silently changed HOT 1
- 3.15 regression: UpdateModelMixin breaks views using Manager objects as queryset HOT 4
- Version 3.15.1 HOT 1
- 3.15 regression: Unset default namespace version suddenly raises 404 HOT 3
- 3.15(.1?) regression: optional fields in serializers are suddenly required (or need explicit None passed) HOT 11
- UniqueConstraint violation_error_message as error response in drf
- rest-framework Supports async class views ?
- 3.15 regression: Serializer validation failed for unique together constraint HOT 2
- Revert changes to `CursorPagination` that caused serious performance regression HOT 3
- Router.register cannot merge with urlpatters HOT 3
- UniqueTogetherValidator does not comply to Database standards
- HyperlinkedModelSerializer doesn't respect SECURE_PROXY_SSL_HEADER settings
- 3.15 is raising required error on model nullable fields HOT 6
- [self-tests] cleanup order of `TestUrlPatternTestCase` HOT 1
- a question about BooleanField HOT 3
- Issue with upgrade from 3.14 to 3.15.1, null values no longer allowed in POST HOT 3
- Incorrect serializer field in quickstart documentation HOT 2
- `coreapi` must be installed for schema support with Python 3.10 HOT 1
- RFE: use `zoneinfo` instead of `pytz` HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-rest-framework.