How to use an attribute of the User model that Django has by default?

0

What I want to do is, be able to use the first name of the user that is registered in the database using the User model that django has by default, my idea is to use its name as a sort of selector with a foreign key, so that when I save my document I refer to that user that I select, here is my model code

models.py

from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import User

class analisis_doc(models.Model):
  nombre_doc = models.CharField(max_length=40)
  referencia=models.CharField(max_length=200)   
  area = models.CharField(max_length=30)
  user = models.ForeignKey(User, default=1)

forms.py

 from django import forms
 from .models import analisis_doc

class analisis_form(forms.ModelForm):
  class Meta:
    model=cargo
    fields=[
     'nombre_doc',
     'referencia',
     'area',
     'user',
    ]

    labels={
     'nombre_doc':('Nombre del documento'),
     'referencia':('Referencias'),
     'area':('Area'),
     'user':('Seleccione nomrbre del usuario'),
    }

    widgets={
     'nombre_doc':forms.TextInput(attrs={'class':'form=control'}),
     'referencia':forms.TextInput(attrs={'class':'form=control'}),
     'area':forms.TextInput(attrs={'class':'form=control'}),
     'user':forms.Select(attrs={'class':'form=control'}),
    }

This is my die that I have of how I could use the name of the table User has django but I think it's wrong, if someone could, please tell me where I'm wrong or how should I do it?

    
asked by Lun 28.05.2018 в 22:37
source

1 answer

2

This way your form would be

from app.models import User

class analisis_form(forms.Form):

    nombre_doc=forms.CharField(widget=forms.TextInput(attrs={
        'class':'form-control',
        }),
           error_messages={'required': 'Proporciona nombre del documento.'})

    referencia = forms.CharField(widget=forms.TextInput(attrs={
        'class':'form-control',
        }),
           error_messages={'required': 'Proporciona referencia'})


    area = forms.CharField(widget=forms.TextInput(attrs={
        'class':'form-control',
        }),
           error_messages={'required': 'Proporciona area.'})

    usuario = forms.ModelChoiceField(widget=forms.Select(attrs={
        'class':"form-control",}),
                              required=True,
                              error_messages={
                              'required':'Seleccione usuario'
                              },
                              queryset=queryset=User.objects.all()

Queryset queries about the user model and brings all users previously registered, to show them in a drop-down list

queryset=queryset=User.objects.all()
    
answered by 28.05.2018 в 23:30