I found this after some further research:
from django.contrib.auth.models import User
class Employee(models.Model):
user = models.OneToOneField(User)
department = models.CharField(max_length=100)
Pregunta
Django creates basic auth tables like auth_user
, auth_user_groups
, auth_user_user_permissions
, etc.
I currently have (1) db with custom tables like Tld
, etc. Tld standing for Top Level Domain.
But, django has created its own tables for permissions above. How can I tie a specific PK in auth_user
to my Tld
table?
I know, I know a FK->PK relationship, but this doesn't come out of the box.
Basically, I want to check if a user has a tld currently (which should just be a JOIN) on the two tables, but as the db currently stands <any-django-out-of-box-auth>
table doesn't know about any custom table I've made.
How do I get this process working, for example, I have a view that creates new records for Tlds:
def Scan(request):
form = SubmitDomain(request.POST or None) # A form bound to the POST data
if request.method == 'POST': # If the form has been submitted...
if form.is_valid(): # If form input passes initial validation...
domainNmCleaned = form.cleaned_data['domainNm'] ## clean data in dictionary
form.save() #save cleaned data to the db from dictionary
try:
return HttpResponseRedirect('/S/?domainNm=' + domainNmCleaned)
except:
raise ValidationError(('Invalid request'), code='300')
else:
form = SubmitDomain()
return render(request, 'myapp/index.html', {
'form' : form
})
How do I get it to also insert the currently logged in User
id in another table called Tld__User
?
So that, when I join Tld
with the system created Auth_User
table I can see if a user has any Tld?
Thank you!
Solución
I found this after some further research:
from django.contrib.auth.models import User
class Employee(models.Model):
user = models.OneToOneField(User)
department = models.CharField(max_length=100)