Guest007
11/10/2015 - 1:46 PM

Model inheritance. Create child by signal after parent's create.

Model inheritance. Create child by signal after parent's create.

# -*- coding: utf-8  -*-
from django.db import models
from organizations.models import Organization
from truckback.core.tools import get_file_name
from versatileimagefield.fields import VersatileImageField
from django.utils.translation import ugettext_lazy as _
from django.db.models.signals import post_save
from django.dispatch import receiver


class Requisites(models.Model):
    """
        Requisites for payments, checkouts, QB integration
    """
    bank = models.CharField(
        _('bank name'),
        blank=True,
        null=True,
        max_length=50
    )


class Client(Organization):
    logo = VersatileImageField(
        _('logo'),
        blank=True,
        null=True,
        upload_to=get_file_name
    )
    phone = models.CharField(
        _('main phone'),
        blank=True,
        null=True,
        max_length=25
    )
    address = models.CharField(
        _('main address'),
        blank=True,
        null=True,
        max_length=125
    )

    requisites = models.OneToOneField("Requisites", blank=True, null=True)

    site = models.URLField(
        verbose_name=u"organization's site",
        blank=True, null=True
    )
    description = models.TextField(
        verbose_name=u"organization's description",
        blank=True, null=True
    )

    objects = models.Manager()


@receiver(post_save, sender=Organization)
def create_orgs_handler(sender, instance, created, **kwargs):
    print(sender, instance, created)
    if not created:
        return
    # Create the client's profile object, only if it is newly created
    client = Client(organization_ptr_id=instance.pk)
    client.__dict__.update(instance.__dict__)
    client.save()