Skip to content

Instantly share code, notes, and snippets.

@Guest007
Last active November 10, 2015 13:48
Show Gist options
  • Save Guest007/feed688da6d30ef7c813 to your computer and use it in GitHub Desktop.
Save Guest007/feed688da6d30ef7c813 to your computer and use it in GitHub Desktop.
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment