django-related-manager

Displaying uploaded images in the template - Django

不想你离开。 提交于 2020-01-22 17:01:30
问题 I am trying to display uploaded images to a template for my imaginary vegetable catalogue. I have a page to add a new vegetable and upload images. The main template is a list of vegetables which will later have thumbnails. Now, when viewing the detail page of the vegetable I want to display its images, but the template is only displaying the string 'VegetableImage Object' in place of an image, even with img tags. I am confused because the template has obviously found the images but they are

sort django queryset by latest instance of a subset of related model

安稳与你 提交于 2019-12-31 02:59:28
问题 I have an Order model and order_event model. Each order_event has a foreignkey to order. so from an order instance i can get: myorder.order_event_set . I want to get a list of all orders but i want them to be sorted by the date of the last event. A statement like this works to sort by the latest event date: queryset = Order.objects.all().annotate( latest_event_date=Max('order_event__event_datetime') ).order_by('latest_event_date') However, what I really need is a list of all orders sorted by

Loop over related model's children in Django template

自作多情 提交于 2019-12-25 01:37:59
问题 I have a model for a company. Then I have a base model for company posts. It contains common posts attributes. An attribute is the company that publishes the posts. It refers to the Company model with a ForeignKey. Finally I have a child model (based on the CompanyPost base model) for posts of type A: class Company(models.Model): name = models.CharField(...) ... class CompanyPost(models.Model): company = models.ForeignKey(Company,...) ... class PostA(CompanyPost): name = ... In a template I

How to get image connected by Foreign Key via Template in django

浪子不回头ぞ 提交于 2019-12-13 01:06:33
问题 i want to get the images form the image model in the template. class Products(models.Model): category = models.ForeignKey(Category) name= models.CharField(max_length=120, unique=True) slug = models.SlugField(unique = True) price = models.IntegerField(default=100) class Image(models.Model): property = models.ForeignKey(Products, related_name='images') image = models.ImageField(upload_to='static/images/home',blank=True,null=True) views.py def index(request): queryset = Products.objects.all()

Django: Programmatically add Groups on User save

大憨熊 提交于 2019-12-12 09:08:20
问题 After a user is saved, I need to make sure that its instance is associated with a group by default. I have found two ways to achieve that: Overriding the model's save() method models.py: from django.contrib.auth.models import AbstractUser, Group class Person(AbstractUser): def save(self, *args, **kwargs): super().save(*args, **kwargs) to_add = Group.objects.get(id=1) # get_or_create is a better option instance.groups.add(to_add) Capturing a post_save signal: signals.py: from django.conf

How to implement a form to add a video using M2M model with through_fields?

点点圈 提交于 2019-12-11 16:22:16
问题 I have this model from this question: class Category(models.Model): category = models.CharField(max_length=50) def __str__(self): return self.category class Tag(models.Model): tag = models.CharField(max_length=50) def __str__(self): return self.tag class Video(models.Model): title = models.CharField(max_length=255) categories = models.ManyToManyField(Category, through='Taxonomy', through_fields=('video', 'category')) tags = models.ManyToManyField(Tag, through='Taxonomy', through_fields=(

Django - edit both sides of a many-to-many relation with generic UpdateView

我只是一个虾纸丫 提交于 2019-12-11 04:47:42
问题 I have a question whether or not it is possible to use the generic UpdateView class to edit "both sides" of a many-to-many relationship. I have the following classes defined in models.py: class SomeCategory(models.Model): code = models.CharField(max_length=5) name = models.CharField(max_length=40) class SomeClass(models.Model): code = models.CharField(max_length=3, unique=True) name = models.CharField(max_length=30, unique=False) age = models.IntegerField(null=False) allowed_categories =

how to: creating a view and serializer for adding, editing, and deleting objects with foreign relationships django rest framework

旧时模样 提交于 2019-12-08 12:35:37
问题 I am having a very hard time connecting all the documentation on django and django rest framework on how to create a view and serializer that allows for a foreign key. edit: I might have an answer here: http://www.django-rest-framework.org/api-guide/relations/#writable-nested-serializers Example I have these models. class SearchCity(models.Model): city = models.CharField(max_length=200) class SearchNeighborhood(models.Model): city = models.ForeignKey(SearchCity, on_delete=models.CASCADE)

Displaying uploaded images in the template - Django

二次信任 提交于 2019-12-03 21:50:26
I am trying to display uploaded images to a template for my imaginary vegetable catalogue. I have a page to add a new vegetable and upload images . The main template is a list of vegetables which will later have thumbnails. Now, when viewing the detail page of the vegetable I want to display its images, but the template is only displaying the string 'VegetableImage Object' in place of an image, even with img tags. I am confused because the template has obviously found the images but they are displayed just as a generic string. models.py class Vegetable(models.Model): title = models.CharField

sort django queryset by latest instance of a subset of related model

南笙酒味 提交于 2019-12-02 01:16:31
I have an Order model and order_event model. Each order_event has a foreignkey to order. so from an order instance i can get: myorder.order_event_set . I want to get a list of all orders but i want them to be sorted by the date of the last event. A statement like this works to sort by the latest event date: queryset = Order.objects.all().annotate( latest_event_date=Max('order_event__event_datetime') ).order_by('latest_event_date') However, what I really need is a list of all orders sorted by latest date of A SUBSET OF EVENTS. For example my events are categorized into "scheduling", "processing