-1

I'm trying to fill a field in my model with the current username in django 1.10.

This is the code that I'm using.

class Archivo(models.Model):
    nombre = models.CharField(max_length = 30)
    documento = models.FileField()
    fecha_creacion = models.DateField(default = timezone.now)
    observaciones = models.CharField(max_length = 20)
    usuario = models.CharField(User.get_username())
    transferencia = models.ForeignKey(User)

The field that I'm trying to fill is "usuario"

And I'm getting the next error: TypeError: get_username() missing 1 required positional argument: 'self'

Am I doing this wrong? By the way, I am not using a view, I am trying to do this directly from the admin site.

I post this image as example that I'm trying to do, the field "Usuario" should be fill automatically with "ROBERTO"...

Example

  • responding to the edit: What exactly are you trying to do from the admin site? – e4c5 Jan 19 '17 at 07:10
  • @e4c5 please check the image – Roberto Franco Jan 19 '17 at 07:15
  • 1
    As I have already mentioned, it's the wrong approach, please read up on Foreign Key relationships (generic) and then https://docs.djangoproject.com/en/1.10/ref/models/fields/#django.db.models.ForeignKey – e4c5 Jan 19 '17 at 07:20

1 Answers1

2

Please post code as text instead of images. You can't add a username like that. What you should really do is to create a ForeingKey or OneToOneField to the User model. To enter the username in your Archivo model is wrong. Then there isn't a clear relationship between your model and your Archivo model. Which means you are not able to use the vast functionality Django provides for working with related models.

Secondly, what if a username changes? What happens to your archivo model? They are left dangling.

Class Archivo(models.Model)
    usero = models.ForeignKey(User)
    ...

Then at the time you need to save an Archivo object, you need to grab it from the request.user

if request.user.is_authenticate():

   arch = Archivo.objects.create(user=request.user, ... )
e4c5
  • 52,766
  • 11
  • 101
  • 134
  • The code is text now. I'll try to explain what I wanna do... I wanna fill the field "usuario" with my current user, for example "betofm92" is the admin and I wanna to fill with that. It is possible? – Roberto Franco Jan 19 '17 at 07:09