Django Query Return One Model as Another - python

I couldn't find a similar question (perhaps because I do not know the correct verbiage), but this is my dilemma. I have two models, one showing a relationship of the other.
I want to be able to compare the 'user_list' and 'friends_list' in the html template so that I can determine whether to not show a user in the "available users" section if a friend relationship already exists.
However, the 'user_list' objects are of the CustomUser model type, and the 'friends_list' objects are of the Friend model type. How can I return the results of the get_friends() method such that I can exclude these friends from the 'user_list' queryset, preferably by pk so I don't have to worry about string comparisons?
Thank you!
Models.py
class CustomUserModel(AbstractBaseUser, PermissionsMixin):
# Simplified for brevity
username = models.CharFiend(_('username'), unique=True)
class Friend(models.Model):
user1 = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete.models.CASCADE, related_name='user1')
user2 = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete.models.CASCADE, related_name='user2')
objects = FriendManager()
class Meta:
unique_together = ('user1', 'user2')
def __str__(self):
return str(self.user2)
Managers.py
class FriendManager(models.Manager):
def is_friend(self, user1, user2):
if not user1 or not user2:
return False
return super(FriendManager, self).get_queryset().filter(user1=user1).filter(user2=user2).exists()
def get_friends(self, user):
return super(FriendManager, self).get_queryset().filter(user1=user)
Views.py
class UserList(ListView):
model = get_user_model()
context_object_name = "object_list"
template_name = "users.html"
def dispatch(self, request, *args, **kwargs):
self.user = get_user(request)
return super(UserList, self).dispatch(request, *args, **kwargs)
def get_queryset(self):
return get_user_model().objects.order_by("-date_joined")
def get_friends(self):
if self.user:
return Friend.objects.get_friends(self.user).order_by("user2")
return None
def get(self, request, *args, **kwargs):
return render(request, self.template_name, {
'user_list': self.get_queryset(),
'friends_list': self.get_friends(),
'user': self.user
})
Users.html
{% extends 'base.html' %}
{% block content %}
<h1>Available Users</h1>
{% for user in user_list %}
{% if user not in friends_list %} <!-- This is my pain-point -->
<p>{{ user }}</p>
{% endif %}
{% endfor %}
<h1>Friends</h1>
{% for friend in friends_list %}
<p>{{ friend }}</p>
{% endfor %}
{% endblock content %}

You can simply change your query to get CustomUser. So the query should be something like CustomeUser.objects.filter(user2__user1=self.user) this will give you the CustomUsers that are friends with self.user. Depending on how you interpret the relation you should also need to query for CustomUser.objects.filter(user1__user2=self.user). These queries should replace your query in the get_friends() method of your view.

Related

Accessing primary key in Django class based view

Accessing primary keys in Django class based view
Let's start from the beginning. I have 2 models, Recipe, and Ingredient. They look like this.
In models.py
class Recipe(models.Model):
name=models.CharField(max_length=20, help_text='Enter the name of this recipe')
description=models.TextField(max_length=75, help_text='Describe your recipe')
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('recipe-detail', kwargs={'pk': self.pk`})
class Ingredient(models.Model):
recipe=models.ForeignKey(Recipe, on_delete=models.CASCADE)
ingredient=models.CharField(max_length=100)
class Meta:
ordering = ['ingredient']
def __str__(self):
return self.ingredient
What I want to be able to do is have a detail view, where I can access the Recipe attributes, like the name and description, as well as, be able to loop through the ingredients. This is what I have working so far:
In views.py
def recipe_detail_view(request, pk):
recipe = get_object_or_404(Recipe, pk=pk)
context = {
'recipe': recipe,
'ingredients': Ingredient.objects.filter(recipe=pk)
}
return render(request, 'recipes/recipe_detail.html', context=context)
In urls.py
# ...
path('recipes/<str:pk>', views.recipe_detail_view, name='recipe-detail')
# ...
In template
<h1 class="title is-1">{{ recipe.name }}</h1>
<p>{{ recipe.description }}</p>
<h3 class="title">Ingredients</h3>
{% for ingredient in ingredients %}
<h4 class="">{{ ingredient.ingredient.title }}</h3>
{% endfor %}
I am wondering how I could turn this into a class based view however. More specifically, I am wondering how I can access and pass in the primary key to the filter like so:
class RecipeDetailView(generic.DetailView):
model = Recipe
template_name = 'recipes/recipe_detail.html'
context_object_name='recipe'
extra_context = {
'ingredients': Ingredient.objects.filter(recipe=pk),
}
Can anyone help?
You can use get_context_data and get_object to get the data you want to your template.
class RecipeDetailView(generic.DetailView):
model = Recipe
template_name = 'recipes/recipe_detail.html'
context_object_name='recipe'
def get_context_data(self, **kwargs)
ctx = super().get_context_data(**kwargs)
ctx['ingredients'] = Ingredient.objects.filter(recipe=self.get_object().pk)
return ctx

How to filter and paginate in ListView Django

I have a problem when I want to paginate the filter that I create with django_filter, in my template it shows me the query set and filter but paginate does not work, I would like to know why this happens and if you could help me.
I'll insert snippets of my code so you can see.
This is my views.py
PD: i have all the necesary imports.
#method_decorator(staff_member_required, name='dispatch')
class EmployeeListView(ListView):
model = Employee
paginate_by = 4
def dispatch(self, request, *args, **kwargs):
if not request.user.has_perm('employee.view_employee'):
return redirect(reverse_lazy('home'))
return super(EmployeeListView, self).dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['filter'] = EmployeeFilter(self.request.GET, queryset = self.get_queryset())
return context
filters.py
import django_filters
from .models import Employee, Accident
class EmployeeFilter(django_filters.FilterSet):
class Meta:
model = Employee
fields = {
'rutEmployee' : ['startswith']
}
You should override get_queryset.This means you have to put your filter in get_queryset like this:
#method_decorator(staff_member_required, name='dispatch')
class EmployeeListView(ListView):
model = Employee
paginate_by = 4
def dispatch(self, request, *args, **kwargs):
if not request.user.has_perm('employee.view_employee'):
return redirect(reverse_lazy('home'))
return super(EmployeeListView, self).dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['filter'] = EmployeeFilter(self.request.GET, queryset = self.get_queryset())
return context
def get_queryset(self):
queryset = super().get_queryset()
return EmployeeFilter(self.request.GET, queryset=queryset).qs
and use object_list instead of filter in employee_list.html like this:
{% for employee in object_list|dictsort:"id" reversed %}
You could also try this:
(A snippet from my source code)
class ModelListView(ListView):
model = YourModel
paginate_by = 4 # Change this if you don't intend to paginate by 4
ordering = model_field_to_order_by
# variable used to know if a match was found for the search made using django_filters
no_search_result = False
def get_queryset(self, **kwargs):
search_results = YourDjangoFiltersForm(self.request.GET, self.queryset)
self.no_search_result = True if not search_results.qs else False
# Returns the default queryset if an empty queryset is returned by the django_filters
# You could as well return just the search result's queryset if you want to
return search_results.qs.distinct() or self.model.objects.all()
def get_query_string(self):
query_string = self.request.META.get("QUERY_STRING", "")
# Get all queries excluding pages from the request's meta
validated_query_string = "&".join([x for x in re.findall(
r"(\w*=\w{1,})", query_string) if not "page=" in x])
# Avoid passing the query path to template if no search result is found using the previous query
return "&" + validated_query_string.lower() if (validated_query_string and not self.no_search_result) else ""
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# Pass to template if you want to do something whenever an empty queryset is return by django_filters
context["no_search_result"] = self.no_search_result
# This is the query string which should be appended to the current page in your template for pagination, very critical
context["query_string"] = self.get_query_string()
context['filter'] = YourDjangoFiltersForm()
return context
In your html template you need to append the querystring passed to your template from the view, a sample is shown below
{% for i in page_obj.paginator.page_range %}
{% if page_obj.number == i %}
<li class="page-item active" aria-current="page">
<span class="page-link">{{ i }}<span class="sr-only">(current)</span></span>
</li>
{% elif i > page_obj.number|add:'-5' and i < page_obj.number|add:'5' %} <li class="page-item"><a
class="page-link" href="?page={{ i }}{{ query_string }}">{{ i }}</a></li>
{% endif %}
{% endfor %}

Django 'NoneType' object has no attribute 'filter'

The task: To assign a single user to the model at a time.
The Error:
Django 'NoneType' object has no attribute 'filter'
The models.py is
class Post(models.Model):
post_tag = models.ForeignKey(ProjectUser, related_name="user_tag", blank=True, null=True,on_delete=models.CASCADE)
Where ProjectUser is:
from django.contrib.auth.models import AbstractUser
class ProjectUser(AbstractUser):
def __str__(self):
return self.username
The .html code is :
% for post_user in objects %}
<form method='POST' action="{% url 'a_tag' post_user.id %}">
{% csrf_token %}
<input type='hidden'>
{% if post_user.id in assigned_user %}
<button type='submit'>Cancel</button>
{% else %}
<button type='submit'>Start</button>
{% endif %}
</form>
The urls.py is
path('<int:tag>', views.tag_analyst, name='a_tag'),
The views.py function is <- Filter, add and remove attributes here are causing the errors
def tag_analyst(request, tag):
post = get_object_or_404(Post, id=tag)
if request.method == 'POST':
if post.post_tag.filter(id=request.user.id).exists():
post.post_tag.remove(request.user)
else:
post.post_tag.add(request.user)
return HttpResponseRedirect(reverse('homepage'))
The views.py class
class View(LoginRequiredMixin, ListView):
context_object_name = "objects"
model = Post
template_name = "page.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['assigned_user'] = self.request.user.user_tag.values_list('id', flat=True)
return context
post_tag is a ForeignKey field for Post, so post.post_tag is an object (or None in this case), but not a Manager. So you can't filter().
You can just check if post.post_tag. This will be True if a user is associated, and False (None) if not.
class Question(models.Model):
webregister = models.ForeignKey(Webregister, on_delete=models.CASCADE)
que = models.CharField(max_length=255,null=True, blank=True)
def __str__(self):
return str(self.pk)
orgs = Question.objects.filter(webregister=module,que__isnull=True)
orgs.delete()

Pre-populating a child models django create form with a parent's ID

I have followed the guidelines from This answer in order to pass Parent pk to the child creation page. At the moment though it is not working and I am seeing the following log.
[14/Jul/2017 13:15:37] "POST /catalog/productstatus/2/create/ HTTP/1.1" 200 4001
I'm not sure what I'm doing wrong, here is the code I currently have.
Models
Models.py
class Product(models.Model):
serial_number = models.CharField(unique=True, max_length=15)
class ProductStatus(models.Model):
serial_number = models.ForeignKey('Product', on_delete=models.CASCADE, null=True)
status = models.CharField(max_length=20, blank=True, default='Stock', help_text='Products status')
date = models.DateTimeField(auto_now_add=True)
View
class ProductStatusCreate(CreateView):
model = ProductStatus
template_name = 'catalog/productstatus_create.html'
form_class = ProductStatusModelForm
def form_valid(self, form):
productstatus = form.save(commit=False)
product_id = form.data['product_id']
product = get_object_or_404(Product, id=product_id)
productstatus.product = product
return super(ProductStatusCreate, self).form_valid(form)
def get_context_data(self, **kwargs):
context = super(ProductStatusCreate, self).get_context_data(**kwargs)
context['s_id'] = self.kwargs['product_id']
return context
def get_success_url(self):
if 'product_id' in self.kwargs:
product = self.kwargs['product_id']
else:
product = self.object.product.pk
return reverse_lazy('product_detail', kwargs={'pk': product})
Forms
class ProductStatusModelForm(forms.ModelForm):
class Meta:
model = ProductStatus
fields = ['status',]
def __init__(self, *args, **kwargs):
self.fields["product"] = forms.CharField(widget=forms.HiddenInput())
super(ProductStatusModelForm, self).__init__( *args, **kwargs)
templates/myapp/product_detail.html
New
urls.py
urlpatterns += [
url(r'^productstatus/(?P<product_id>\d+)/create/$', views.ProductStatusCreate.as_view(), name='productstatus_create'),
]
productstatus_create.html
{% extends "base_generic.html" %}
{% block content %}
<h2>New Product Status</h2>
</br>
<form action="" method="post">
{% csrf_token %}
<table>
<input type=hidden id="id_product" name="product" value="{{ s_id }}">
{{ form }}
</table>
<input type="submit" value="Submit" />
</form>
</br>
{% endblock %}
When looking at the page's source the value does get populated but when I submit the form nothing happens.
Why do you have views.ProductInstanceCreate.as_view() in your urls.py but the view you show is called ProductStatusCreate? Are you sure you are using the right view?
You are creating a 'product' hidden field in your form, but not providing a value for it anywhere. Your template output then has two product fields, and the latter (blank) is taken, so returns an error saying it is required.
None of this outputting the product ID to the template in order to read it back in is necessary - you always have the ID available to you in the URL kwargs.
You can get rid of your get_context_data, and the extra field code in the Form and template. Your form_valid can be something like:
def form_valid(self, form):
product = get_object_or_404(Product, id=self.kwargs['product_id'])
form.instance.product = product
return super().form_valid(form)
And product_id will always be in self.kwargs, so your get_success_url can be shorter too:
def get_success_url(self):
product = self.kwargs['product_id']
return reverse('product_detail', kwargs={'pk': product})

Django Wizard, multiple forms in one step

In documentation of Django Wizard i found code like this:
{{ wizard.management_form }}
{% if wizard.form.forms %}
{{ wizard.form.management_form }}
{% for form in wizard.form.forms %}
{{ form }}
{% endfor %}
{% else %}
{{ wizard.form }}
{% endif %}
So I am wondering how can i add multiple forms to single step of wizard
Make one of your forms a Formset containing the rest of the forms you need. You don't need to necessarily use a ModelFormset, you can subclass the base class and create the forms manually.
This is now deprecated use this link: https://github.com/vikingco/django-formtools-addons
I wanted to share my settings if would be any help to anyone:
class BaseImageFormSet(BaseModelFormSet):
def __init__(self, *args, **kwargs):
super(BaseImageFormSet, self).__init__(*args, **kwargs)
self.queryset = Images.objects.none()
ImageFormSets = modelformset_factory(Images, formset=BaseImageFormSet, fields=('picture',), extra=2)
form_list = [("step1", CategoryForm),
("step2", CityForm),
("step3", (
('lastform', LastForm),
('imageform', ImageFormSets)
))
]
templates = {"step1": "create_post_category.html",
"step2": "create_post_city.html",
"step3": "create_post_final.html"}
class OrderWizard(SessionMultipleFormWizardView):
file_storage = FileSystemStorage(location=os.path.join(settings.MEDIA_ROOT, 'photos'))
def get_template_names(self):
return [templates[self.steps.current]]
def render(self, forms=None, **kwargs):
forms = forms or self.get_forms()
context = self.get_context_data(forms=forms, **kwargs)
#print(forms[1](queryset = Images.objects.none()))
return self.render_to_response(context)
def done(self, form_list, form_dict, **kwargs):
form_data_dict = self.get_all_cleaned_data()
#print(form_data_dict)
result = {}
instance = Post()
#print(form_dict)
for key in form_dict:
form_collection = form_dict[key]
#print(form_collection)
for key in form_collection:
form = form_collection[key]
print('printing form %s' % key)
#if isinstance(form, forms.ModelForm):
if key == 'lastform':
post_instance = form.save(commit=False)
nodes = form_data_dict.pop('nodes')
city = form_data_dict.pop('city')
post_instance.save()
post_instance.category.add(nodes)
post_instance.location.add(city)
print('lastfome as esu ')
if key == 'imageform':
for i in form_data_dict['formset-step3']:
picture = i.pop('picture')
images_instance = Images(post=post_instance, picture=picture)
images_instance.save()
return render_to_response('create_post_done.html', {
'form_data': result,
#'form_list': [form.cleaned_data for form in form_list],
})
I've implemented an extension to the Django Wizard, which supports multiple Forms in one wizard step:
https://pypi.python.org/pypi/django-multipleformwizard

Categories