Federico Ribero
Federico Ribero

Reputation: 309

Query for retrieve data with user fk Django 1.11

I'm trying to retrieve data from user. I have my model like this:

from django.db import models

from django.contrib.auth.models import User

Create your models here.

class informacionFacturacion(models.Model):
    usuario = models.ForeignKey(User)
    apellidos = models.CharField(max_length=100, default="editar")
    nombres = models.CharField(max_length=100, default="editar")
    telefono = models.CharField(max_length=100, default="editar")
    email = models.EmailField(default="editar", null=False)
    direccion_1 = models.CharField(max_length=100, default="editar")
    direccion_2 = models.CharField(max_length=100, null=True, blank=True)
    provincia = models.CharField(max_length=100, default="editar")
    ciudad = models.CharField(max_length=100, default="editar")
    codigoPostal = models.CharField(max_length=100, default="editar")
    empresa = models.CharField(max_length=100, default="editar")

    def __str__(self):
        return self.usuario

My form for update user information:

from .models import informacionFacturacion

class informacionFacturacionForm(ModelForm):
    class Meta:
        model = informacionFacturacion
        fields = [
            "usuario",
            "apellidos",
            "nombres",
            "telefono",
            "email",
            "direccion_1",
            "direccion_2",
            "provincia",
            "ciudad",
            "codigoPostal",
            "empresa",
        ]

And in my view I have my query like this

from django.contrib.auth.decorators import login_required
from .models import informacionFacturacion
from .forms import informacionFacturacionForm
@login_required
def datosPersonales(request):
    form = informacionFacturacionForm(request.POST or None)
    if form.is_valid():
        instance = form.save(commit=False)
        instance.save()
    query = informacionFacturacion.objects.filter(usuario=request.user)
    context = {
        "titulo": "Datos personales | Cadenas Giordanino S.R.L" + request.user.username,
        "body_class": "class= sidebar_main_open sidebar_main_swipe",
        "form": form,
        "infoFacturacion": query,
    }
    template = "micuenta/datosPersonales.html"
    return render(request, template, context)

And this QuerySet is empty.

I need to retrieve this data in the user profile

**UPDATE: ** Full code on post.

**UPDATE 2: ** For displaying the user data on profile, im using a "For loop". This data, is retrieved in "value=" attr of html inputs. If the user has no data, the form dosnt show.

This is the way I wanna show the data. I populated this form from the same form u see here. This is the way I wanna show the data. I populated this form from the same form u see here.

Here's when i enter for first time to my profile with no data Here's when i enter for first time to my profile with no data

Thanks a lot.

Upvotes: 1

Views: 117

Answers (1)

Daniel van Flymen
Daniel van Flymen

Reputation: 11551

Are you sure that request.user is the user you've linked your anotherModel to? If you aren't currently logged in then request.user will be an instance of AnonymousUser. See more in the Documentation: https://docs.djangoproject.com/en/1.11/ref/request-response/#django.http.HttpRequest.user

You can use the Django Shell for testing your models:

$ python manage.py shell

Then make some models:

from django.contrib.auth.models import User
from models import AnotherModel

# Grab a User
user = User.objects.first()

# Create a new anotherModel, linking the user
my_model = AnotherModel(
    user=user,
    address="whatever"
)
my_model.save()

my_model.user == user
>>> True

Upvotes: 1

Related Questions