Giter Club home page Giter Club logo

Comments (4)

apragacz avatar apragacz commented on May 21, 2024

Hi @joshvillbrandt

Not sure if I understand your question. Assuming you are using the default User model (this is set by AUTH_USER_MODEL Django setting) the DefaultRegisterUserSerializer will infer that it will require following fields: username, first_name, last_name, email, password, password_confirm, which is illustrated by this testcase:

def test_ok(self):
serializer_class = registration_settings.REGISTER_SERIALIZER_CLASS
serializer = serializer_class(data={})
field_names = {f for f in serializer.get_fields()}
self.assertEqual(
field_names,
{'id', 'username', 'first_name', 'last_name', 'email',
'password', 'password_confirm'},
)

(id is readonly so it's not actually used)

You can limit the fields by adding other fields you want to hide to ‘USER_HIDDEN_FIELDS’ setting. Even better, you can use ‘USER_PUBLIC_FIELDS’ setting to specify which fields exactly you want to have in register endpoint (again DefaultRegisterUserSerializer is inferring these; it will add the password, password_confirm fields automatically even if they are not listed in ‘USER_PUBLIC_FIELDS’ setting).

If on the other hand, you want additional fields in the register endpoint, which cannot be inferred from your user model, the only option I see is to replace DefaultRegisterUserSerializer by using 'REGISTER_SERIALIZER_CLASS'. You can consider subclassing DefaultRegisterUserSerializer as it contains all the validation logic like for instance testing for non-trivial passwords.

Hope this helps in any way. If not, you are more than welcome to clarify!

from django-rest-registration.

joshvillbrandt avatar joshvillbrandt commented on May 21, 2024

Hmm. I am a bit confused. When I hit my "accounts/register" endpoint right now, let's say with no data, the response I get is:

{"password":["This field may not be blank."],"username":["This field may not be blank."]}

My desired behavior is that the response also lists first_name, last_name, and email as "This field may not be blank." Are you saying that this should already be the case?

They way that I am interpreting the test_ok test that you embedded is simply that it verifies that the fields are supported by the serializer, but not anything about whether they are required or not.

If it makes a difference, I am only using Django 1.11. Maybe Django changed which fields were required by default. I'll that that out.

from django-rest-registration.

joshvillbrandt avatar joshvillbrandt commented on May 21, 2024

Alright, it appears that I was in fact sending first_name, last_name, and email which were not tripping Django's "blank" validation. I suppose the best way to change this would indeed be to set AUTH_USER_MODEL to a custom user model and to have those fields of min_length validators.

For the time being, I'm just having my javascript change empty strings to nulls. It's not server-side validated, but that is okay with me in this scenario.

Thanks for your time, @apragacz!

from django-rest-registration.

lock avatar lock commented on May 21, 2024

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

from django-rest-registration.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.