Django button linked to database

I’m trying to solve a problem that i don’t find on google ( apparently ).
I got a Django Project which allows People to get registered.
I got my form which is something like this:

forms.py

class RegistrationForm(UserCreationForm):
"""docstring for RegistrationForm"""
email = forms.EmailField(required=True)
studio_associato = forms.CharField(required=True)
nome = forms.CharField(required=True)
cognome = forms.CharField(required=True)	

class Meta: # define a metadata related to this class
	model = User
	fields = (
		'username',
		'nome',
		'cognome',
		'studio_associato',
		'password1',
		'password2'
	)

I managed to correctly store and visualize the User in the DB and the admin page.

What i’m trying to get is to let this field ‘studio_associato’ be a field that when a User register himself through the registration form, he have to specify from which (in Italian Studio = Office ) Office he’s working for.
Each User can only work with an Office unlike Office could have obviously more Users linked to it.

My final product i’m trying to achieve is a field: “Studio associato” (showed during the registration form of the User) which displays all the Offices registered till that moment and let you Click or Search a specific Office to link that user for.
Note that my html page is just a normal recall to the form i linked above:

register.html

<body>
<h1>Register!</h1>
<form method="post">
	{% csrf_token %}
	{{form.as_p}}
	<button type="submit">Submit</button>

</form>

(Note that studio_associato is just some name, so a text field)

Thanks in advance, hope i explained it clearly

So there are two different answers possible based upon your specific situation.

Either, these offices are a known, complete list, and aren’t ever going to change - in which case you can set them up as a list of choices for that field.

Or, if the list of offices needs to be more dynamic, you might want to create an “Office” model, which contains information about those offices, and define a Many-to-one relationship between the User and the Office. (The examples on that page are a good fit if you think in terms of “Article” being your Users and “Reporter” being your Office - each Reporter can write multiple articles but each article was written by only one reporter.)

Ken

Thanks for your response!

Offices are dynamic, at a certain time there could be one ore more offices in the main list, as long as the Users could join, more Offices could join too.

May you help me implementing that somehow?

Thanks Ken

If you haven’t already worked through the full Django Tutorial, that’s your first step. Again, the parallels here are a good one between Question/Choice and Office/User.

If you “worked” through it by just copy/pasting the code fragments into files, then I would suggest going back through it and type out all the components. The manual act of typing every line of code helps you understand what’s going on line-by-line.

If you have worked through it completely, then you should know enough to get started with your idea. Go ahead and put some code together, and if you still have questions, feel free to post your code and ask again.

Ken

1 Like