Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

.save() in django



from __future__ import print_function
from django.db import models
from django.db.models import signals
from django.db.models.signals import pre_save, post_save
from django.dispatch import receiver

__author__ = 'sobolevn'

class CustomManager(models.Manager):
    def get_queryset(self):
        super_query = super(models.Manager, self).get_queryset()
        print('Manager is called', super_query)
        return super_query


class ExtraObject(models.Model):
    name = models.CharField(max_length=30)

    def __unicode__(self):
        return self.name


class TestModel(models.Model):

    name = models.CharField(max_length=30)
    key = models.ForeignKey('ExtraObject')
    many = models.ManyToManyField('ExtraObject', related_name='extras')

    objects = CustomManager()

    def save(self, *args, **kwargs):
        print('save() is called.')
        super(TestModel, self).save(*args, **kwargs)

    def __unicode__(self):
        # Never do such things (access by foreing key) in real life,
        # because it hits the database.
        return u'{} {} {}'.format(self.name, self.key.name, self.many.count())


@receiver(pre_save, sender=TestModel)
@receiver(post_save, sender=TestModel)
def reicever(*args, **kwargs):
    print('signal dispatched')


Comment

.save() in django

>>> one_entry = Entry.objects.get(pk=1)
Comment

PREVIOUS NEXT
Code Example
Python :: or in if statement python 
Python :: add an item to a dictionary python 
Python :: for _ in range() in python 
Python :: adding array to array python 
Python :: python if greater than and less than 
Python :: scikit learn 
Python :: python print an array 
Python :: oops python self and making an object of a class and calling function 
Python :: sub function python 
Python :: add new element to python dictionary 
Python :: tuples vs list 
Python :: how to make a label in python 
Python :: import from parent directory in python setup 
Python :: //= python meaning 
Python :: mod in python 
Python :: how to use inputs in python 
Python :: Unreadable Notebook: jupyter 
Python :: buble short 
Python :: Patch loop runner _run_once 
Python :: input list in function and display column in dataframe python 
Python :: why is python so populair 
Python :: arcpy save map layer to feature class 
Python :: iif python 
Python :: was en francais 
Python :: index operator with if and elif statement in python 
Python :: for loop with 2 variables python 
Python :: sns nan matrix 
Python :: add legend to px.choropleth map python 
Python :: dependency parser tags 
Python :: reverse the order of list elements 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =