英文:
Import Django Model Serializer and Use it in Model's Save Method
问题
I am getting ImportError: cannot import name 'Tenant' from 'tenant_profile.models'. I am assuming it's due to circular import.
models.py
from .serializers import TenantSerializer
class Tenant(models.Model):
# model fields
def save(self, *args, **kwargs):
_ = super().save(*args, **kwargs)
data = TenantSerializer(self).data
# do something
return _
serializers.py
from .models import Tenant
class TenantSerializer(serializers.ModelSerializer):
class Meta:
model = Tenant
fields = '__all__'
A Pythonic way to fix would be much appreciated.
英文:
Peace,
I am getting
> ImportError: cannot import name 'Tenant' from 'tenant_profile.models'
I am assuming it's due to circular import.
# models.py
from .serializers import TenantSerializer
class Tenant(models.Model):
# model fields
def save(self, *args, **kwargs):
_ = super().save(*args, **kwargs)
data = TenantSerializer(self).data
# do something
return _
# serializers.py
from .models import Tenant
class TenantSerializer(serializers.ModelSerializer):
class Meta:
model = Tenant
fields = '__all__'
A Pythonic way to fix would be much appreciated.
答案1
得分: 3
Import the serializer in the method:
# models.py
# no <s>from .serializers import TenantSerializer</s>
class Tenant(models.Model):
# model fields
def save(self, *args, **kwargs):
<b>from .serializers import TenantSerializer</b>
_ = super().save(*args, **kwargs)
data = TenantSerializer(self).data
# do something
return _
# serializers.py
from .models import Tenant
class TenantSerializer(serializers.ModelSerializer):
class Meta:
model = Tenant
fields = '__all__'</code></pre>
英文:
Import the serializer in the method:
<pre><code># models.py
no <s>from .serializers import TenantSerializer</s>
class Tenant(models.Model):
# model fields
def save(self, *args, **kwargs):
<b>from .serializers import TenantSerializer</b>
_ = super().save(*args, **kwargs)
data = TenantSerializer(self).data
# do something
return _
serializers.py
from .models import Tenant
class TenantSerializer(serializers.ModelSerializer):
class Meta:
model = Tenant
fields = 'all'</code></pre>
答案2
得分: 2
由于"models.py"和"serializers.py"之间的循环导入引起的错误,当两个或多个模块尝试相互导入时会发生这种情况。
在这种情况下,"models.py"正在从"serializers.py"中导入TenantSerializer,而"serializers.py"正在从"models.py"中导入"Tenant"。
要解决此问题,您可以在model.py的save函数内导入TenantSerializer:
from .serializers import TenantSerializer
class Tenant(models.Model):
# 模型字段
def save(self, *args, **kwargs):
from .serializers import TenantSerializer # 将导入放在函数内
_ = super().save(*args, **kwargs)
data = TenantSerializer(self).data
# 做一些操作
return _
希望这对您有所帮助。
英文:
the error caused by circular import between "models.py" and "serializers.py", this happens when two or more modules try to import eachother.
in this case models.py is importing TenantSerializer from serializers.py and serializers.py is importing "Tenant" from "models.py"
to resolve this you can import TenantSerializer inside save function in model.py:
from .serializers import TenantSerializer
class Tenant(models.Model):
# model fields
def save(self, *args, **kwargs):
from .serializers import TenantSerializer # Move import inside the function
_ = super().save(*args, **kwargs)
data = TenantSerializer(self).data
# do something
return _
I hope this helps you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论