Coder Social home page Coder Social logo

Comments (11)

FraCata00 avatar FraCata00 commented on June 12, 2024 1

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 docs:
image

from django-rest-framework.

PunkFleet avatar PunkFleet commented on June 12, 2024

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.

FraCata00 avatar FraCata00 commented on June 12, 2024

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?

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.

PunkFleet avatar PunkFleet commented on June 12, 2024

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?

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.

FraCata00 avatar FraCata00 commented on June 12, 2024

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.

PunkFleet avatar PunkFleet commented on June 12, 2024

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?

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.

FraCata00 avatar FraCata00 commented on June 12, 2024

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 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?

Yeah, there are mine apps.py and signals config: apps.py

from 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.

PunkFleet avatar PunkFleet commented on June 12, 2024

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 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?

Yeah, there are mine apps.py and signals config: apps.py

from 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.

FraCata00 avatar FraCata00 commented on June 12, 2024

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.

PunkFleet avatar PunkFleet commented on June 12, 2024

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.

FraCata00 avatar FraCata00 commented on June 12, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.