django-models

How do I store the uploaded file inside a folder having the name from the model field in django?

为君一笑 提交于 2020-01-05 12:30:42
问题 Using the upload_to argument of FileField I want to store the file uploaded by the user in a folder named TestName ie the test name entered by the user. I've written the following code, but it creates a folder named "models.CharField(max_length=255)" instead. How can I fix this? from django.core.validators import FileExtensionValidator from django.db import models from django.contrib.auth.models import User class TestInfo(models.Model): TestName = models.CharField(max_length=255) MaxMarks =

search and export the data in .csv format using django

旧街凉风 提交于 2020-01-05 10:14:26
问题 In my application i am using a search function which search and display the output.I include search for fromdate and todate , Keyword search etc. I want to export the searched result in a .csv file.Currently i had written a function called csv_export and exporting all the report in .csv.I want to know how to export the searched item in a .csv file. forms.py for search class SearchFilterForm(Form): location = forms.ChoiceField(widget=forms.Select(), choices='',required=False) type = forms

With django-tables2 how do I display a model @property?

我只是一个虾纸丫 提交于 2020-01-05 10:08:14
问题 I have a @property in my model which is basically a string of several values in the model combined. @property def mfg_link(self): return ''.join('http://mfg.com/model/' + str(self.model_numer)) I can add this mfg_link to the list_display on the admin model and it works fine, but its not showing up in the generated table when I pass the queryset to the table, the other fields show up fine. This seems like something obvious, but unfortunately the couple of hours of searching didn't help. Thanks

Django get ImageField path before saving image

不想你离开。 提交于 2020-01-05 09:14:25
问题 I'm trying to save an image using the chunks method to handle the case where a user tries to upload a large image: destination = open(incident.Image.path, 'wb+') for chunk in request.FILES['image'].chunks(): destination.write(chunk) destination.close() My problem is I can't get the filepath without first saving something in the Image field like so: fileName = str(int(time.time() * 1000)) imageName = fileName + '.jpg' incident.Image.save(imageName, request.FILES['image']) My question is how

Django filter query - doesn't work

不羁的心 提交于 2020-01-05 08:43:26
问题 I have problems with my Django, I want to write a very simple query but it doesn't work. Model: class Games(models.Model): name = models.CharField(max_length=128) path_to_folder = models.CharField(max_length=256) description = models.TextField() cover = models.URLField() def __str__(self): return self.name I am trying something like this (It should find itself in my opinion): >>> from gamepanel.models import Games >>> e = Games.objects.all() >>> print (e) [<Games: Call Of Duty 4>] >>> e[0]

Missing metaclass in Django 1.5

心不动则不痛 提交于 2020-01-05 08:15:36
问题 How do you override a model's metaclass in Django 1.5? I was overriding the metaclass on some models inheriting from an abstract model so I could set appropriate choices. e.g. class BaseModel(models.Model): field_with_choices = models.CharField(max_length=100) class Meta: abstract = True class MyModelMetaClass(BaseModel.__metaclass__): def __new__(cls, *args, **kwargs): new_class = super(MyModelMetaClass, cls).__new__(cls, *args, **kwargs) field = new_class._meta.get_field('field_with_choices

Django Template not displaying model data

ぐ巨炮叔叔 提交于 2020-01-05 07:41:26
问题 I've looked through different tutorials and Stack Overflow questions and I'm not sure why I'm still running into this issue: I am running into an issue with displaying my model data onto my template, I can see that the python code is executing, but no matter what I've tried I can't get my data to come through, my relevant code snippets are below: models.py class GoogleData(models.Model): placeID = models.CharField(max_length=999) name = models.CharField(max_length=200) phoneNumber = models

Django Template not displaying model data

柔情痞子 提交于 2020-01-05 07:41:09
问题 I've looked through different tutorials and Stack Overflow questions and I'm not sure why I'm still running into this issue: I am running into an issue with displaying my model data onto my template, I can see that the python code is executing, but no matter what I've tried I can't get my data to come through, my relevant code snippets are below: models.py class GoogleData(models.Model): placeID = models.CharField(max_length=999) name = models.CharField(max_length=200) phoneNumber = models

Django using model of one app into other app's model

此生再无相见时 提交于 2020-01-05 07:39:11
问题 Salam and hi, I am using django and breaking two parts of project in two apps. However there is a reference field of one table as foreign key of other in terms of db. So how can I use one app's model primary key as foreign key of another app's model? And if is not possible then should I just make single app of all such apps. I am actually thinking apps as different modules/components of project, may be I will be adding some apps in future. thanks 回答1: Either import the model from the other

How do you access parent and children relationships through the ORM with a conditional on the parent where record child exist?

微笑、不失礼 提交于 2020-01-05 07:34:08
问题 I am trying to create a query through the Django ORM which is a straight join. I am trying to extract only records from parent table that have an entry in the child table, In addition, I would like to add a conditional on the parent table. Here are the sample model: class Reporter(models.Model): first_name = models.CharField(max_length=64) last_name = models.CharField(max_length=64) class Article(models.Model): pub_date = models.DateField() headline = models.CharField(max_length=200) content