开发者

Make all Django models inherit from certain class

开发者 https://www.devze.com 2023-02-07 19:32 出处:网络
I wrote a certain function that I\'d like all my Django models to have. What\'s the best way to make all my models inherit from a certain class?

I wrote a certain function that I'd like all my Django models to have. What's the best way to make all my models inherit from a certain class?

What I tried didn't work. I made a new class like this:

from django.db import models

class McifModel(models.Model):
    pass

And then I did this in another model:

from django.db import models, connection, transaction
from mcif.models.mcif_model import McifModel

class Customer(mcif.models.McifModel):
    id = models.BigIntegerField(primary_key=True)
    customer_number = models.CharField(unique=True, max_length=255)
    social_security_number = models.CharField(unique=True, max_length=33)
    name = models.CharField(unique=True, max_length=255)
    phone = models.CharField(unique=True, max_length=255)
    deceased = models.IntegerField(unique=True, null=True, blank=True)
    do_n开发者_Python百科ot_mail = models.IntegerField(null=True, blank=True)
    created_at = models.DateTimeField()
    updated_at = models.DateTimeField()

But I got this error:

Traceback (most recent call last):
  File "./import.py", line 6, in <module>
    from mcif.models import GenericImport, Customer, CSVRow
  File "/home/jason/projects/mcifdjango/mcif/models/__init__.py", line 4, in <module>
    from mcif.models.account_address import AccountAddress
  File "/home/jason/projects/mcifdjango/mcif/models/account_address.py", line 2, in <module>
    from mcif.models.account import Account
  File "/home/jason/projects/mcifdjango/mcif/models/account.py", line 2, in <module>
    from mcif.models.customer import Customer
  File "/home/jason/projects/mcifdjango/mcif/models/customer.py", line 4, in <module>
    class Customer(mcif.models.McifModel):
NameError: name 'mcif' is not defined


Because you didn't import mcif -- you imported McifModel. Try this:

from mcif.models.mcif_model import McifModel

class Customer(McifModel):
    ...
0

精彩评论

暂无评论...
验证码 换一张
取 消