django-models

Django FileField default file

最后都变了- 提交于 2021-02-16 05:00:10
问题 I have a model which contains FileField as below class Employer(models.Model): logo = models.FileField(storage=FileSystemStorage(location=settings.MEDIA_ROOT), upload_to='logos') The question is how can I add a default file like "{{ MEDIA_ROOT}}/logos/anonymous.jpg" to this filefield ? 回答1: You can specify the default file to use for that field as follows: class Employer(models.Model): logo = models.FileField(storage=FileSystemStorage(location=settings.MEDIA_ROOT), upload_to='logos', default=

Django: Duplicated logic between properties and queryset annotations

只愿长相守 提交于 2021-02-15 14:56:25
问题 When I want to define my business logic, I'm struggling finding the right way to do this, because I often both need a property AND a custom queryset to get the same info. In the end, the logic is duplicated. Let me explain... First, after defining my class, I naturally start writing a simple property for data I need: class PickupTimeSlot(models.Model): @property def nb_bookings(self) -> int: """ How many times this time slot is booked? """ return self.order_set.validated().count() Then, I

what is the correct way to override the save method in django?

扶醉桌前 提交于 2021-02-11 17:22:52
问题 I have an image model where I can upload images and I want to optimize them with pillow, I did that but there is three problems: the images doesn't get saved in the correct folder. django has a feature when there is two files with the same name django adds a random string to its name but now with two images with the same name only one gets uploaded. the original images gets uploaded too. class Images(models.Model): image1 = models.ImageField(upload_to='images/%Y/', validators=

what is the correct way to override the save method in django?

核能气质少年 提交于 2021-02-11 17:21:56
问题 I have an image model where I can upload images and I want to optimize them with pillow, I did that but there is three problems: the images doesn't get saved in the correct folder. django has a feature when there is two files with the same name django adds a random string to its name but now with two images with the same name only one gets uploaded. the original images gets uploaded too. class Images(models.Model): image1 = models.ImageField(upload_to='images/%Y/', validators=

How to populate a ManyToMany Field in Django using a .csv file?

守給你的承諾、 提交于 2021-02-11 14:16:22
问题 I have 2 models Influencer and Category. Influencer has a many to many relationship with Category.The models look like this: class Category(models.Model): name = models.CharField(max_length=100) def __str__(self): return self.name and class Influencer(models.Model): full_name = models.CharField('Full Name',max_length=100) username = models.CharField('Username',max_length=100,unique=True) photo = models.ImageField(upload_to = 'photos/%Y/%m/%d/',blank=True) location_city = models.CharField(

Django DateField format impossible to validate and save to model

让人想犯罪 __ 提交于 2021-02-11 12:26:26
问题 Old issue, I know. But I still could not find a working solution so I may have missed something obvious. Here is my form class AddPatientForm(forms.Form): last_name = forms.CharField(label='Nom', max_length=40) first_name = forms.CharField(label='Prénom', max_length=40) birthday = forms.DateField(label='date de naissance', widget=forms.DateInput(format='%d/%m/%Y',attrs={'placeholder': '31/03/1989'}), input_formats=['%d/%m/%Y',]) It follows this format convention and has to do so . Here is my

How to get related field's value as ManyToMany fields' choice label in Django

余生长醉 提交于 2021-02-11 12:24:22
问题 I have two related models with one of the fields of a model having ManyToManyField relationship with the other model like shown here: Models class Processes(models.Model): x_process = models.CharField(primary_key=True, ...) x_process_text = models.CharField(max_length=35, verbose_name='Short Desc') class RelStatuses(models.Model): rel_status = models.CharField(primary_key=True, ...) rel_status_text = models.CharField(max_length=55, verbose_name='Short Desc') app_process_compo = models

AttributeError on successful redirection in django?

谁说我不能喝 提交于 2021-02-11 06:42:37
问题 Redirection on successful POST request from Host View. def product_page_view(request, prod_slug, color=None, size=None): ... ... prod = Product.objects.filter(slug=prod_slug).first() seller = prod.seller ... .... order_form = Buy_now_form() if request.method == "POST": order_form = Buy_now_form(request.POST) if order_form.is_valid(): # Function for processing POST request and return order order = buy_now_view(request, prod.slug, color, size) # Redirection return redirect(reverse('product

Django filter query if filter parameter exists

情到浓时终转凉″ 提交于 2021-02-10 19:55:21
问题 I'm trying to create a django filter with multiple filter parameters (ex. name, age, height). However, I only want to filter by a parameter if it exists... In my init: def __init__(self, name=None, age=None, height=None): self.name = name self.age = age self.height = height in my query: Person.objects.filter(name=self.name, age=self.age, height=self.height) However, the problem is since the parameters are optional in the constructor, there is a chance that the filter is looking for None

Django: Get last record by ID [many-to-one relationship]

一个人想着一个人 提交于 2021-02-10 18:31:53
问题 I'm trying to get the last record in one table connected to another one using many-to-one relationship in django models. Here's my django models: class DataCollecttion(models.Model): default_name = models.CharField(max_length=100) class NameHistory(models.Model): old_name = models.CharField(max_length=100) collection_data = models.ForeignKey(DataCollection, on_delete=models.CASCADE, null=True) Here I created a sample data for DataCollection table: And here's the sample data for NameHistory