하나의 JSON 객체에 모든 정보를 유지하려고합니다. "establecimientos"관계를 삭제하면 오류가 발생하지 않지만 "establecimientos"를 포함하면 다음 오류가 발생합니다.Django TypeError : 'formas_pago'는이 함수에 대한 잘못된 키워드 인수입니다.
Error: TypeError: 'formas_pago' is an invalid keyword argument for this function
이 오류는 라인에서 발생 negocio = Negocio.objects.create(**validated_data)
내 시리얼 클래스의
시리얼 클래스
이 (establecimientos_data = validated_data.pop ('establecimientos이 라인까지 제대로 serializadora 클래스 작품이다 '))
class NegocioSerializer(ModelSerializer):
establecimientos = EstablecimientoSerializer(many=True)
class Meta:
model = Negocio
fields = ('id','nombre', 'slug', 'fecha_creado', 'tipos_negocio','formas_pago', 'propietario', 'establecimientos')
read_only_fields = ('propietario',)
def create(self, validated_data):
establecimientos_data = validated_data.pop('establecimientos')
negocio = Negocio.objects.create(**validated_data)
for establecimiento_data in establecimientos_data:
Establecimiento.objects.create(negocio=negocio, **establecimiento_data)
return negocio
모델 클래스
class Negocio(models.Model):
nombre = models.CharField(max_length=255)
slug = models.CharField(max_length=255)
logo = models.FileField(upload_to=upload_logo_to)
fecha_creado = models.DateTimeField(auto_now=True)
propietario = models.ForeignKey(User)
tipos_negocio = models.ManyToManyField('TipoNegocio')
formas_pago = models.ManyToManyField('FormaPago')
def __str__(self):
return self.nombre
def get_establecimiento(self):
establecimientos = Establecimiento.objects.filter(negocio=self)
return establecimientos
class TipoNegocio(models.Model):
tipo = models.CharField(max_length=255)
def __str__(self):
return self.tipo
class Establecimiento(models.Model):
nombre = models.CharField(max_length=255)
direccion = models.TextField()
telefono = models.CharField(max_length=255)
negocio = models.ForeignKey('Negocio', related_name='establecimientos')
def __str__(self):
return u'{0} - {1}'.format(self.negocio, self.nombre)
def get_horarios(self):
horarios = Horario.objects.filter(establecimiento=self)
return horarios
class FormaPago(models.Model):
forma_pago = models.CharField(max_length=255)
def __str__(self):
return self.forma_pago
JSON
{
"nombre": "nuevo",
"slug": "nuev",
"tipos_negocio": [
2,
3
],
"formas_pago": [
1
],
"establecimientos": [{
"nombre": "Matriz",
"direccion": "asasasas",
"telefono": "0997739485",
"horarios": []
},
{
"nombre": "bbb",
"direccion": "sassdd",
"telefono": "445588",
"horarios": []
}]
}
감사 목록에있는 모든 개체를 얻을, 그리고
formas_pago
필드에 추가 할 수 있고,이 해결책이 될,하지만 수없는 이유가 단지 ManyToMany, 장고 때 저장하는 방법을 알고 있습니까? 이 예에서는 "forma_pago"가 자동으로 추가되면 모델 "Negocio"의 "Establecimiento"를 제거합니다. – user3417635