How can I use the field of a foreignkey that I have in the extended dl User that Django brings?

0

I'm using the user that brings default Django and expanded it with a foreignkey, here's the code:

#*---usuario/models.py

from django.db import models
from django.contrib.auth.models import AbstractUser
from app.organigrama.models import unidad

class User(AbstractUser):
     Fecha_ingreso = models.DateField(auto_now = False , auto_now_add = False)
     Unidad=models.ForeignKey(unidad, null=True, blank=False)


class Meta:
    db_table = 'auth_user'

Code of my form.py

  from django.contrib.auth.models import User
  from django.contrib.auth.forms import UserCreationForm

  class registroUser(UserCreationForm):
   class Meta:
    model= User
    fields=[
           'username',
           'first_name',
           'last_name',
           ]
    labels={
           'username':'Nombre de usuario',
           'first_name':'Nombre',
           'last_name':'Apellido',
            }

Code of my sight

 from django.contrib.auth.models import User
 from django.contrib.auth.forms import UserCreationForm
 from django.views.generic import CreateView
 from app.usuario.forms import registroUser
 from django.shortcuts import render

 class registro(CreateView):
    model = User
    template_name = "usuarios/registrarUsuarios.html"
    form_class = registroUser

Code of my Url

  url(r'^registro/', views.registro, name='registro'),

And finally my page registerUsuario.html

   <form method="post">

  {% csrf_token %}


<div class="row">
    <div class="col-md-8 col-md-offset-2">
        <div class="form-group">
            <label>Nombre</label>
            <input class="form-control" type="text" name="first_name">
        </div>
    </div>
</div>
<div class="row">
    <div class="col-md-8 col-md-offset-2">
        <div class="form-group">
            <label >Apellidos</label>
            <input class="form-control" type="text" name="last_name">
        </div>
    </div>
</div>
<div class="row">
    <div class="col-md-8 col-md-offset-2">
        <div class="form-group">
            <label >Nombre de usuario</label>
            <input class="form-control" type="text" name="username">
        </div>
    </div>
</div>

<div class="row">
    <div class="col-md-8 col-md-offset-2">
        <div class="form-group">
            <label >Contraseña</label>
            <input class="form-control" type="password" name="password1">
        </div>
    </div>
</div>
<div class="row">
    <div class="col-md-8 col-md-offset-2">
        <div class="form-group">
            <label >Confirmar contraseña</label>
            <input class="form-control" type="password" name="password2">
        </div>
    </div>
</div>

<div class="row">
    <div class="col-md-8 col-md-offset-2">
        <div class="form-group">
            <button class="btn btn-primary "type="submit">Guardar</button>
        </div>
    </div>
</div>

I would like to know how to use the fields that my foreignkey brings and put them in my form, by the way the field that my foreignkey brings is the name of the unit to which a user belongs and that can be selectable because they already have registered some data in my foreignkey "unit", if someone can, please help me or clarify the doubt I have

    
asked by Lun 21.05.2018 в 23:50
source

0 answers