Open gabrielfgularte opened 2 years ago
as quick glance I see that you have
user as required (null=False) on Account
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
but in schema it accepts None:
account: AccountSchema = None
also Account was not created for the user - that is general source of the error
I guess you need to have some code that automatically creates account for each created user or something (or use null=True
)
Got you. My project assumes that a user can live without an account. So an account can be created later. I'll try with null=True. Thank you very much!
@vitalik Same issue with null=True in the user field =/
I have a workaround. If I put a resolver on the Schema using hasattr
it works:
class UserSchema(ModelSchema):
account: Optional[AccountSchema] = None
class Config:
model = User
@staticmethod
def resolve_account(obj):
if hasattr(obj, 'account'):
return obj.account
return None
yeah, I guess this is the best solution for now for OneToOneField case
Are there any plans to tackle this?
Are there any plans to tackle this?
@rkulinski the resolve_
seems working good here ? what's your vision ?
maybe this also can work
class User(models.Model):
email = models.EmailField(unique=True)
def get_account(self):
if hasattr(obj, 'account'):
return obj.account
...
class UserSchema(ModelSchema):
account: Optional[AccountSchema] = Field(None, alias='get_account')
the problem with automating this is that we cannot be sure of developer mind here if onetoone relations must be enforced or optional at runtime
That's how we work around this.
Describe the bug Models that are OneToOne related are producing an
RelatedObjectDoesNotExist
error using ModelSchema.This code produces the following exceptions:
Versions (please complete the following information):