Reputation: 359
I'm a little messed here with this thing... Lost few hours and did try lots of variants of this code, but with no success.
I need create an related object called ContaCorrente
to each user as Usuario
when it creates an account.
Those are my models:
class Usuario(models.Model):
"""Classe que ira gerir o cliente final, cadastrado via APP ou Webapp"""
nome = models.CharField(max_length=60)
sobrenome = models.CharField(max_length=60)
telefone = models.CharField(max_length=20)
.... FOR THE SAKE OF BREVITY
def __str__(self):
return self.nome + ' ' + self.sobrenome
class ContaCorrente(models.Model):
"""Controle financeiro do Cliente/Usuario cadastrado"""
pontos = models.DecimalField(max_digits=10, decimal_places=2, default=0)
saldo_reais = models.DecimalField(max_digits=10, decimal_places=2, default=0)
usuario = models.OneToOneField(Usuario, on_delete=models.CASCADE, related_name='conta_corrente')
And this is my Serializers:
class ContaCorrenteSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = ContaCorrente
fields = ('pontos', 'saldo_reais', 'usuario_id')
class UsuariosSerializer(serializers.HyperlinkedModelSerializer):
conta_corrente = ContaCorrenteSerializer(read_only=True)
id = serializers.ReadOnlyField()
class Meta:
model = Usuario
fields = (
'id',
'nome',
'sobrenome',
'telefone',
...
FOR THE SAKE OF BREVITY
...
'updated_at',
'conta_corrente'
)
def perform_create(self, serializer):
conta = ContaCorrente.objects.create(usuario_id=self.kwargs.get('pk'), saldo_reais=0, pontos=0)
conta.save()
serializer.save()
return serializer
I did try lots and lots of variants of this code, but can't find where it explodes.
The Usuario
model get persisted, but the ContaCorrente
not! Someone has any help to give? Thanks!
Upvotes: 2
Views: 2530
Reputation: 47374
There is not such method as perform_create
in parent classes.
Try to use create
instead. Source code.
Upvotes: 1