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!