django-models

Generating a non-sequential ID/PK for a Django Model

僤鯓⒐⒋嵵緔 提交于 2019-12-31 08:45:06
问题 I'm on the cusp of starting work on a new webapp. Part of this will give users pages that they can customise in a one to many relationship. These pages naturally need to have unique URLs. Left to its own devices, Django would normally assign a standard AUTOINCREMENT ID to a model. While this works fantastically, it doesn't look great and it also makes pages very predictable (something that isn't desired in this case). Rather than 1, 2, 3, 4 I would like set-length, randomly generated

How can i set the size of rows , columns in textField in Django Models

丶灬走出姿态 提交于 2019-12-31 08:44:06
问题 I have this Model summary = models.TextField() But I want to have only 4 rows and 15 columns. Also if I do that do I need to install database again or not. 回答1: TextField is a type of field, which when used by a ModelForm by default uses the Textarea widget. Fields deal with backend storage, widgets with front-end editing. So you need to specify the widget you want to go with your field. You want to create a ModelForm and specify the widget you want to use, per the documentation: from django

django models selecting single field

吃可爱长大的小学妹 提交于 2019-12-31 08:42:28
问题 I have a table/models called Employees and I would like to get all rows of a single field as a queryset. I know I can do it like this (hope I'm doing this right even): emp_list = Employees.objects.get(all) emp_names = emp_list.eng_name Would query the database for all fields and using only one? Is there a better (faster) way of doing this? 回答1: Employees.objects.values_list('eng_name', flat=True) That creates a flat list of all eng_name s. If you want more than one field per row, you can't do

django models selecting single field

♀尐吖头ヾ 提交于 2019-12-31 08:41:32
问题 I have a table/models called Employees and I would like to get all rows of a single field as a queryset. I know I can do it like this (hope I'm doing this right even): emp_list = Employees.objects.get(all) emp_names = emp_list.eng_name Would query the database for all fields and using only one? Is there a better (faster) way of doing this? 回答1: Employees.objects.values_list('eng_name', flat=True) That creates a flat list of all eng_name s. If you want more than one field per row, you can't do

Django request get parameters

不想你离开。 提交于 2019-12-31 08:28:12
问题 In a Django request I have the following: POST:<QueryDict: {u'section': [u'39'], u'MAINS': [u'137']}> How do I get the values of section and MAINS ? if request.method == 'GET': qd = request.GET elif request.method == 'POST': qd = request.POST section_id = qd.__getitem__('section') or getlist.... 回答1: You can use [] to extract values from a QueryDict object like you would any ordinary dictionary. # HTTP POST variables request.POST['section'] # => [39] request.POST['MAINS'] # => [137] # HTTP

Django request get parameters

自作多情 提交于 2019-12-31 08:27:08
问题 In a Django request I have the following: POST:<QueryDict: {u'section': [u'39'], u'MAINS': [u'137']}> How do I get the values of section and MAINS ? if request.method == 'GET': qd = request.GET elif request.method == 'POST': qd = request.POST section_id = qd.__getitem__('section') or getlist.... 回答1: You can use [] to extract values from a QueryDict object like you would any ordinary dictionary. # HTTP POST variables request.POST['section'] # => [39] request.POST['MAINS'] # => [137] # HTTP

Django Imagefield not working properly via ModelForm

我怕爱的太早我们不能终老 提交于 2019-12-31 08:04:19
问题 I'm certain I'm doing something really obviously stupid, but I've been trying to figure it out for a few hours now and nothing is jumping out at me. I'm using a ModelForm so I can expose a few fields from a model for editing. 2x ImageField, 1x TextField. The Form is processed and the TextField works. The two ImageFields do not work and they're why I'm here today. I'm using Django 1.0.2 Here's the relevant code (ask if you need more -- and I'm not including the HTML because that part appears

Django Imagefield not working properly via ModelForm

爱⌒轻易说出口 提交于 2019-12-31 08:04:18
问题 I'm certain I'm doing something really obviously stupid, but I've been trying to figure it out for a few hours now and nothing is jumping out at me. I'm using a ModelForm so I can expose a few fields from a model for editing. 2x ImageField, 1x TextField. The Form is processed and the TextField works. The two ImageFields do not work and they're why I'm here today. I'm using Django 1.0.2 Here's the relevant code (ask if you need more -- and I'm not including the HTML because that part appears

Why does MYSQL DB return a corrupted value when averaging over a Django models.DateTimeField?

天大地大妈咪最大 提交于 2019-12-31 07:10:32
问题 I'm running a Django application on top of a MySQL (actually MariaDB) database. My Django Model looks like this: from django.db import models from django.db.models import Avg, Max, Min, Count class myModel(models.Model): my_string = models.CharField(max_length=32,) my_date = models.DateTimeField() @staticmethod def get_stats(): logger.info(myModel.objects.values('my_string').annotate( count=Count("my_string"), min=Min('my_date'), max=Max('my_date'), avg=Avg('my_date'), ) ) When I run get

How to render two form fields as one field in Django forms?

淺唱寂寞╮ 提交于 2019-12-31 05:58:08
问题 I'm working on Django application where I need to render a form differently. The form contains multiple fields related to a person. 1. Firstname 2. Lastname 3. Email 4. Address 5. City 6. Country 7. Phone 8. Pincode There are two flows in my application. In the first flow I can render all the form fields and save them when the user enters some data and submit. But in the second flow, I need to display only three fields as below. 1. Name - *Combination of Firstname and Lastname* 2. Email 3.