django-models

Filtering django DatetimeField__date not working

冷暖自知 提交于 2020-12-08 08:35:02
问题 According to this document that was added on v1.9 we can able to query a DateTimeField by date without time. Examples are: Entry.objects.filter(pub_date__date=datetime.date(2005, 1, 1)) Entry.objects.filter(pub_date__date__gt=datetime.date(2005, 1, 1)) But it is not working for me: class MilkStorage(models.Model): .... created_at = models.DateTimeField(null=False) Usage from datetime import date MilkStorage.objects.filter(created_at__date=date.today()) It returns an empty queryset <QuerySet [

Django - download file from FileField()

人走茶凉 提交于 2020-12-06 16:52:57
问题 I'm struggling with the following problem. I have a database model with FileField() . models.py class InputSignal(models.Model): input_file = models.FileField(upload_to='signals/', null=False, ) A view that displays records from this table. It also supports deleting specific rows. views.py def storage_list(request): signals = InputSignal.objects.filter(author=request.user) if request.method == 'DELETE': id = json.loads(request.body)['id'] signal = get_object_or_404(InputSignal, id=id) signal

Django - download file from FileField()

女生的网名这么多〃 提交于 2020-12-06 16:52:25
问题 I'm struggling with the following problem. I have a database model with FileField() . models.py class InputSignal(models.Model): input_file = models.FileField(upload_to='signals/', null=False, ) A view that displays records from this table. It also supports deleting specific rows. views.py def storage_list(request): signals = InputSignal.objects.filter(author=request.user) if request.method == 'DELETE': id = json.loads(request.body)['id'] signal = get_object_or_404(InputSignal, id=id) signal

How to upload multiple files from the Django admin?

心已入冬 提交于 2020-12-04 06:40:32
问题 I want to upload multiple files in the Django admin, without having to place multiple FileField fields. The user can be able to manage the files in a simple way; delete or change each uploaded file but upload several at once. the solution I see viable is to use several filefield but the problem is, i dont know how many files the user will upload def case_upload_location(instance, filename): case_name = instance.name.lower().replace(" ", "-") file_name = filename.lower().replace(" ", "-")

Two different submit buttons in same form in Django

牧云@^-^@ 提交于 2020-12-03 07:31:53
问题 I have an UpdateView in Django. I have just a normal submit button. When the object is updated correctly it redirects to an object list via success_url . Can I make two different submit buttons: One button which submits and redirects to objects list page (ListView) and another button which submits and redirects to the object detail page (DetailView)? I don't know how to do this in a smart way. 回答1: Since you're submitting to the same place, and only want to change the redirect destination

Django model naming convention

一笑奈何 提交于 2020-11-30 06:26:09
问题 What is the preferred naming convention for Django model classes? 回答1: Django models are just Python classes, so the Python naming conventions detailed in PEP-8 apply. For example: Person Category ZipCode If Django fails to pluralize the class name properly when creating the corresponding table, you can easily override the pluralization by setting a custom verbose_name_plural field in an inner META class. For example: class Story(models.Model): ... class Meta: verbose_name_plural = "stories"

Django model naming convention

∥☆過路亽.° 提交于 2020-11-30 06:24:35
问题 What is the preferred naming convention for Django model classes? 回答1: Django models are just Python classes, so the Python naming conventions detailed in PEP-8 apply. For example: Person Category ZipCode If Django fails to pluralize the class name properly when creating the corresponding table, you can easily override the pluralization by setting a custom verbose_name_plural field in an inner META class. For example: class Story(models.Model): ... class Meta: verbose_name_plural = "stories"

Django model field contain strange attribute _(“private”)

笑着哭i 提交于 2020-11-29 10:26:26
问题 Currently I have been learning Django and while reading, I have come across the below code block which I don't understand. private = models.BooleanField( _('private'), default=False, help_text=_('theme is available ONLY for the site.'), ) The above line of code contains _('private') and I am not able to understand what it does. I know about using _ for translation-related stuff. Why attribute name not declared for _("private") ? I have tried to find the answer online but have been unable.

Django model field contain strange attribute _(“private”)

佐手、 提交于 2020-11-29 10:25:52
问题 Currently I have been learning Django and while reading, I have come across the below code block which I don't understand. private = models.BooleanField( _('private'), default=False, help_text=_('theme is available ONLY for the site.'), ) The above line of code contains _('private') and I am not able to understand what it does. I know about using _ for translation-related stuff. Why attribute name not declared for _("private") ? I have tried to find the answer online but have been unable.

Retrieve a list of matching objects from a range of ids in Django

邮差的信 提交于 2020-11-29 09:53:36
问题 I want to achieve something relatively simple: I want to retrieve all objects from my model given a range of ids (for eg, retrieve the lines 5 to 10 from a book's chapter). Right now in my views.py, I've: def line_range(request, book_id, chapter_id, line_start, line_end): book_name = get_object_or_404(Book, id = book_id) chapter_lines = [] for i in range (int(line_start), int(line_end)+1): chapter_lines .append(Line.objects.get(book = book_id, chapter = chapter_id, line = i)) return render_to