导入Django模型序列化器并在模型的保存方法中使用它。

huangapple go评论73阅读模式
英文:

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):
    &lt;b&gt;from .serializers import TenantSerializer&lt;/b&gt;

    _ = 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.

huangapple
  • 本文由 发表于 2023年7月24日 19:49:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76754197.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定