Django rest framework best practices

Viewed 524

I was wondering what would be the best approach . I've been agonizing on what is the best way to accomplish a given task. It is a project with a react frontend and django rest framework backend. I am working on the backend with the django rest framework .

Task:

  1. To query a user by their phonenumber which is stored as the username field.
  2. To add a user to a profile model and add an extra field which is required by the profile model
  3. To create the new profile with the user as the profiles user .

e.g

Profile Model:

User - ForeignKey
ExtraField - Charfield

My question is which of these solutions is better .

  1. Should I build a url like this : domain/users/{phonenumber} and post the Extra field and query the user like this.

def get_object(self):
     return get_object_or_404(User,username=self.kwargs['phonenumber'])

user= get_object()

or

  1. Should I post both the phonenumber and the extrafield and query the user from the backend like this.
user = get_object_or_404(User,username=request.data.get('phonenumber'))

both work but which would be the best practice if it even matters at all.

1 Answers

If you just starting a django project the best way would be to create a custom user model with only fields you need for your user. See docs here.

Related