Return in a ListView information of 2 models filtered by the authenticated user

3

Hi, I'm new to django and would like to know how I can return the information of an authenticated user of 2 models in a ListView

This is my code:

models.py

from django.contrib.auth.models import User

class ModelOne1(models.Model):
    VALUE1 = 'value1'
    VALUE2 = 'value2'
    DAYS = (
        (VALUE1, 'value1'),
        (VALUE2, 'value2'),
    )
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    days = models.CharField(max_length=7, choices=DAYS, default=VALUE1)

    def is_upperclass(self):
        return self.days in (self.VALUE1, self.VALUE2)


class ModelTwo2(models.Model):
    VALUE1 = 'value1'
    VALUE2 = 'value2'
    DAYS = (
        (VALUE1, 'value1'),
        (VALUE2, 'value2'),
    )
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    days = models.CharField(max_length=7, choices=DAYS, default=VALUE1)

    def is_upperclass(self):
        return self.days in (self.VALUE1, self.VALUE2)

Create this other model in this same file by passing the other 2 models as values.

class Allmodels(models.Model):
    model1 = models.ForeignKey(ModelOne1, on_delete=models.CASCADE, null=True)
    model2 = models.ForeignKey(ModelTwo2, on_delete=models.CASCADE, null=True)

forms.py

from django import forms
from .models import ModelOne1, ModelTwo2
from django.contrib.auth.models import User

class FormOfModel1(forms.ModelForm):
    class Meta:
        model  = ModelOne1
        fields = ['days']
        widgets = {
            'user': forms.ModelChoiceField(queryset=User.objects.all()),
            'days': forms.Select(attrs={'class':'x'}),
        }


class FormOfModel2(forms.ModelForm):
     class Meta:
          model  = ModelTwo2
          fields = ['days']
          widgets = {
              'user': forms.ModelChoiceField(queryset=User.objects.all()),
              'days': forms.Select(attrs={'class':'x'}),
          }

views.py

class AllView(ListView):
     model = Allmodels
     template_name = 'x/allview_list.html'

     def get_queryset(self):
         return Allmodels.objects.filter(user=self.request.user)

and finally this is the view I want to return, but I do not know how to filter the information of the 2 models so that the authenticated user can see it. Thank you very much friends!

    
asked by Yamamoto AY 20.08.2018 в 15:35
source

1 answer

1

If what you want to do is use your Allmodels to filter the logged in user in ModelOne1 and ModelOne2 then you can do this in your view:

class AllView(ListView):
     model = Allmodels
     template_name = 'x/allview_list.html'

     def get_queryset(self):
         return Allmodels.objects.filter(
             model1__user=self.request.user,
             model2__user=self.request.user
         )

Both the model1 field and model2 are related to the user. With those lookups you can get the user information for each one.

To iterate in the template you use the object_list that the ListView gives you:

{% for object in object_list %}
    {{object.model1.user}}
    {{object.model1.days}}
    {{object.model2.user}}
    {{object.model2.days}}
{% endfor %}
    
answered by 20.08.2018 в 16:04