I have two following models (with OneToOne relation):
class Company(BaseModel):
name = models.CharField(max_length=64)
address_street = models.CharField(max_length=64)
address_city = models.CharField(max_length=64)
address_zipcode = models.CharField(max_length=6)
# ...many other fields here...
class InvoicingData(BaseModel):
company = models.OneToOneField(Company, null=True, blank=True)
company_name = models.CharField(max_length=64)
address_street = models.CharField(max_length=64)
address_city = models.CharField(max_length=64)
address_zipcode = models.CharField(max_length=6)
I want to auto-create InvoicingData model instance, when new Company is being added. I wanted to use signal to do so:
@receiver(post_save, sender=Company)
def create_invoicing_data(sender, instance, created, **kwargs):
if created:
# WHAT HERE?
Is there any simple, automated way to copy shared fields values from one model to another?
@receiver(post_save, sender=Company)
def create_invoicing_data(sender, instance, created, **kwargs):
if created:
invdata = InvoicingData(company=instance)
# set any other fields on invdata if desired
invdata.save()
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments