0

I have People app. I have Person model that has username field. When I add a person and that person does not have an account yet, I click the little plus sign next to the username field and I'm presented with a New User form.

That is okay.

Now I want to create users in this manner: The username should be in a form of {firstname}.{lastname}[.{serial}] fo that John Smith will have username john.smith and another John Smith would be john.smith.1.

How can I pass the firstname and lastname to the new user form? Also: How to check if there's another user with that username and append the serial number to it?

Thank you

lmojzis
  • 551
  • 7
  • 17
  • I'm a little confused about how the models relate. Please post your model code (and any relevant form code), so that we can see how these models relate. – Brian Dant Jan 24 '14 at 14:29
  • If my answer below doesn't help, please let me know so I can modify it. – Brian Dant Jan 24 '14 at 15:07

1 Answers1

0

If the first and last names are being entered (but not yet submitted) on the Person form, and you want those values to show up in the new user form view, then you'll have to:

a) use JavaScript to grab the first name and last name when the user hits that add button (the "plus button"), then
b) pass those values into a custom view that dynamically creates the {firstname}.{lastname}.[serial], then
c) assign that value to form.fields['username'].initial

When the user sees the next form, they will have the value you entered pre-populated. This logic should probably go in a custom form method (whichever form manages the username).

This strategy brings up other issues you'll have to address:
1) What if the user changes the First and Last name in the original Person form, before submitting? (#2 below might solve this)
2) Every time the first or last name are changed, you'll have to update the username on the other model

Other things to consider:

You could do this using a custom user model (docs, tutorial), and put those first and last names in the same model, which could eliminate the custom view creation, allow you to create the username dynamically in one page, and make it easier to sync them.

Community
  • 1
  • 1
Brian Dant
  • 4,029
  • 6
  • 33
  • 47