django-ninja icon indicating copy to clipboard operation
django-ninja copied to clipboard

[Question] Duplicated name: 'id' in params

Open migratis opened this issue 3 years ago • 2 comments
trafficstars

Hello, I want to create a new Account object, but at compilation I have this error :

File "/usr/local/lib/python3.9/site-packages/ninja/signature/details.py", line 167, in _args_flatten_map
raise ConfigError(
ninja.errors.ConfigError: Duplicated name: 'id' in params: 'account' & 'account'

here is my code :

class UserSchemaOut(ModelSchema):
    class Config:
        model = models.User
        model_fields = ['id', 'email', 'first_name',
            'last_name', 'language', 'birthdate']

class BankSchema(ModelSchema):
    class Config:
        model = models.Bank
        model_fields = ['id', 'name', 'code']

class AccountSchema(ModelSchema):
    bank: BankSchema
    user: UserSchemaOut
    class Config:
        model = models.Account
        model_fields = ['id', 'custom_name', 'iban']


@api_controller('')
class AccountController(ControllerBase):

    @http_post('/account/create', auth=JWTTokenUserAuth(), response=AccountSchema)
    def saveAccount(self, request, account: AccountSchema = Form(...)):
        try:
            userId = request.user.id
            user = models.User.objects.get(pk=userId)
            bank = models.Bank.objects.get(pk=account.bank)
            account.bank = bank
            account.user = user
            new_account = models.Account(**account.dict())
            new_account.save()

            return JsonResponse({"detail": [{"success": ["account-successfuly-saved"]}]})
        except ValidationError as e:
            if (new_account.id is not None): new_account.delete()
            return JsonResponse({"detail": formatErrors(e.message_dict)}, status=422)

what is my mistakes ?

Kind regards

Philippe

migratis avatar Sep 15 '22 14:09 migratis

@migratis

well it looks like you are using some library on top of django ninnja - so I'm not 100% sure what's going on

but most likely .dict() does not return what you spect here:

            account.bank = bank
            account.user = user
            new_account = models.Account(**account.dict())

I think it should be like:

            user = models.User.objects.get(pk=userId)
            bank = models.Bank.objects.get(pk=account.bank)
            new_account = models.Account(bank=bank, user=user, **account.dict())

vitalik avatar Sep 15 '22 18:09 vitalik

Hi @vitalik , Thanks for your help, it did not fix the error but will probably fix subsequent errors I could have after... I finally manage to compile by removing the 'id' field of UserSchemaOut. I hope that I will not need it for the rest of the application, since the user id is always obtained by the authentication. Perhaps this fix make sense for you...

migratis avatar Sep 15 '22 20:09 migratis