Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django autofixture generating test data with foreign key
I have using django-autofixture to generate some random data for my database instance. My model is as follows: class DummyModel(models.Model): name = models.CharField(max_length=100) description = models.CharField(max_length=150, blank=True) time_points = models.PositiveIntegerField() more_text = models.CharField(max_length=100, blank=True) image_type = models.ForeignKey(ImageTypeModel, null=False, blank=False, default='') class Meta: db_table = "dummy" So, I have a foreign key which should always be present and it cannot be NULL. I also have created instances for this ImageTypeModel using the fixtures functionality in Django. I deleted everything and migrated fresh and verified that the ImageTypeModel database table exists and is populated. The ImageTypeModel is defined as: class ImageTypeModel(models.Model): name = models.CharField(max_length=100) dims = models.IntegerField() class Meta: db_table = "imagetypes" def __str__(self): return self.name def __unicode__(self): return self.name Now I attempt to create some random data using the command: python manage.py loadtestdata myapp.DummyModel:30 However, this returns me this error: django.db.utils.IntegrityError: NOT NULL constraint failed: dummy.image_type_id I am at a loss as to why it should be and have tried deleting/replicating the project to a fresh location but no go. -
django-allauth: submitting form after social login
The current flow for visitors with django-allauth is if the visitor fills in a form, if they are not already authenticated they are shown a login modal with option of facebook login or standard signup form, with the original form data being saved in ACCOUNT_SIGNUP_FORM_CLASS=SignupForm class SignupForm(forms.Form): def signup(self, request, user): new_user = Profile() new_user.user = user new_user.save() # if form data provided with signup save it. model = self.cleaned_data['model'] if model: # save the form This works fine if the user signs up using the standard signup form, but if they signup using facebook social account the values from the form are now empty strings i.e. self.cleaned_data['model'] -
Thumbnail for ImageField in django model, override save
I am trying to create a thumbnail from ImageField and save it to my model. I am using this as reference: http://www.yilmazhuseyin.com/blog/dev/create-thumbnails-imagefield-django/ Files are created correctly, I get no errors but fields are not saved in model. I think i could messed things up in save method. What am I doing wrong? Here is my code: def get_avatar_path(instance, filename): # file will be uploaded to MEDIA_ROOT/user_<id>/<filename> return 'avatars/{0}/{1}'.format(instance.id, filename) class User(AbstractUser): # First Name and Last Name do not cover name patterns # around the globe. name = models.CharField(_('Name of User'), blank=True, max_length=255) avatar = models.ImageField(upload_to=get_avatar_path, blank=True) small_avatar = models.ImageField(upload_to=get_avatar_path, blank=True) def create_thumbnail(self): # If there is no image associated with this. # do not create thumbnail if not self.avatar: return # Set our max thumbnail size in a tuple (max width, max height) THUMBNAIL_SIZE = (200, 200) DJANGO_TYPE = self.avatar.file.content_type print DJANGO_TYPE if DJANGO_TYPE == 'image/jpeg': PIL_TYPE = 'jpeg' FILE_EXTENSION = 'jpg' elif DJANGO_TYPE == 'image/png': PIL_TYPE = 'png' FILE_EXTENSION = 'png' elif DJANGO_TYPE == 'image/gif': PIL_TYPE = 'gif' FILE_EXTENSION = 'gif' # Open original photo which we want to thumbnail using PIL's Image image = Image.open(StringIO(self.avatar.read())) # use our PIL Image object to create the thumbnail, which already … -
How to format date in Highcharts
I'm trying to render an chart in Django with Highcharts using an plugin for grouping by day, week and month. The working example can be found on this fiddle. The code that works is as follows: var categories = [ '2017-03-11 00:00:00 GMT-0500 (EST)', '2017-03-12 00:00:00 GMT-0500 (EST)' ].map(function(date) { let formatOptions = { month: '2-digit', day: '2-digit', year: 'numeric' }; return new Date(date).toLocaleDateString(undefined, formatOptions); }); Highcharts.chart('container', { chart: { type: 'line' }, dateRangeGrouping: true, title: { text: 'Average Temperature Date' }, xAxis: { categories: categories }, yAxis: { title: { text: 'Temperature (°C)' } }, plotOptions: { line: { dataLabels: { enabled: true }, enableMouseTracking: false } }, series: [{ name: 'City A', data: [7.0, 14.0] }] }); This is working example. But I have an problem with formatting the date. What if I want the day bigger than 12. For example '2017-03-23 00:00:00 GMT-0500 (EST)'. Thus var categories = [ '2017-03-11 00:00:00 GMT-0500 (EST)', '2017-03-23 00:00:00 GMT-0500 (EST)' ].map(function(date) { let formatOptions = { month: '2-digit', day: '2-digit', year: 'numeric' }; return new Date(date).toLocaleDateString(undefined, formatOptions); }); If I try this I get an error (Invalid time value): Any idea how to solve this? -
MS Azure doesn't install virtualenv for Django project. error: Unable to find vcvarsall.bat
I have a problem with deploy my Django project to MS Azure.I working on Linux env. I made files runtime.txt, requirements.txt and push it to Azure, but I have trouble when the service tried to install mysqlclient. Azure tried install this lib from source code, not wheel file. After this situation I have added .whl file to my project in folder 'wheelhouse' and changed requirement.txt, next I pushed it and took the same error: error: Unable to find vcvarsall.bat. Here log file from Azure, structure my project, requirements.txt |project/ |--app/ |--wheelhouse/ |----mysqlclient-1.3.10-cp35.whl reuirements.txt --find-links wheelhouse mysqlclient==1.3.10 Django==1.10.4 Log from azure Command: "D:\home\site\deployments\tools\deploy.cmd" Handling python deployment. Detected requirements.txt. You can skip Python specific steps with a .skipPythonDeployment file. Detecting Python runtime from runtime.txt Detected python-3.4 Found compatible virtual environment. Pip install requirements. Downloading/unpacking mysqlclient==1.3.10 (from -r requirements.txt (line 2)) Running setup.py (path:D:\home\site\wwwroot\env\build\mysqlclient\setup.py) egg_info for package mysqlclient warning: no files found matching 'GPL-2.0' Requirement already satisfied (use --upgrade to upgrade): Django==1.10.4 in d:\home\site\wwwroot\env\lib\site-packages (from -r requirements.txt (line 3)) Installing collected packages: mysqlclient Running setup.py install for mysqlclient building '_mysql' extension error: Unable to find vcvarsall.bat Complete output from command D:\home\site\wwwroot\env\Scripts\python.exe -c "import setuptools, tokenize;file='D:\home\site\wwwroot\env\build\mysqlclient\setup.py';exec(compile(getattr(tokenize, 'open', open)(file).read().replace('\r\n', '\n'), file, 'exec'))" install --record D:\local\Temp\pip-8m_f_kxa-record\install-record.txt … -
Source extra variables in django_manage task
This is my django_manage task: - name: Run Django database migrations django_manage: command: migrate app_path: "{{ project_path }}" virtualenv: "{{ virtualenv_path }}" settings: "{{ django_settings_file }}" environment: "{{ django_environment }}" when: run_django_db_migrations is defined and run_django_db_migrations tags: django, migrate, deploy The file extra_env_vars has some more variables: #!/bin/sh export ADMIN_GROUP_NAME=support export SECRET='sdfadsfsfa' I want to run . {{ project_path }}/extra_env_vars within the task environment, so that the django-migrate can access these variables. Additional info The reason why I am exporting the variables seperately is because I do not want to add secret keys to Git. This task is from a role in my project ansible-django-postgres-nginx. Do check for general structure. -
How to properly avoid JOINs to count associated elements in Django?
In a "many to one" relationship, is it a good Django practice to store the count of related elements on the "one" side, or Django had a better mechanism reducing the overhead ? Say we have two tables : "Posts" and "Likes" and we can have several likes on one post. My DBA instinct is to add a nb_likes field on "Posts" and increment it in the Like(post_id,user_id...) method. The default Django ORM behavior is to make a "JOIN" which seems to be very wasteful. -
Simple Django Model (Add to Cart)
This is my first Django project and I am trying to implement add-to-cart features. What changes should I make in this model so that multiple items can be added into "Order"? from django.db import models from django.utils import timezone # Create your models here. class Order(models.Model): customer = models.ForeignKey('Customer') ordered_item = models.ForeignKey('OrderQuantity', on_delete=models.CASCADE, null=True) address = models.TextField() created_date = models.DateTimeField(default=timezone.now) class Customer(models.Model): first_name = models.CharField(max_length=50) last_name = models.CharField(max_length=50) email = models.EmailField() phone = models.CharField(max_length=50) def __str__(self): return self.first_name class Item(models.Model): name = models.CharField(max_length=50) price = models.DecimalField(default=0.00, max_digits=100, decimal_places=2) description = models.TextField(null=True) summary = models.TextField(null=True) type = models.CharField(max_length=50, null=True) brand = models.CharField(max_length=50, null=True) weight = models.DecimalField(default=0.00, max_digits=100, decimal_places=3) picture = models.ImageField(null=True, upload_to='images/') created_date = models.DateTimeField(default=timezone.now) def __str__(self): return self.name class OrderQuantity(models.Model): product = models.ForeignKey('Item') quantity = models.PositiveIntegerField() -
django list of images
i'd like to create a model of a good for an online shop. the model should contain a number of images. i have two approaches to do that: 1) class Good(models.Model): ... class Img(models.Model): img = models.ImageField(... good = models.ForeignKey("Good", ... 2) class Good(models.Model): img = models.ForeignKey('ChainImg', ... class ChainImg(models.Model): img = models.ImageField(... anotherimg = models.ForeignKey("ChainImg", ... as i understand to get all images for a good: in case of 1) rows in the "Img" table will be filtered for id of the good, and if i have 100 goods, the table will be filtered 100 times for 100 different ids. in case of 2) if an average good has 10 pictures chained, the "Img" table will be queried 10 times to return 10 rows by their ids. So for 100 goods "Img" table will be queried about 1000 times by ids, but no filtering is needed. ChainImg could also point to itself and create a problem. but that can be avoided. Which approach would be better? Faster? More efficient? Correct? -
Django REST API public and private
In Django app how can we define two sets of API for public and private Ex: public REST api url pointing gunicorn to 127.0.0.0.1:8000 and configure nginx to serve the rest api on port 80. This is default. How can I run another REST api process on different port inside private network to communicate with internal systems. ? Thanks. -
Foreignkey of Django User model to another model
I know how to define user profile model in django as follows. class Profile(models.Model): user = models.OneToOneField(User) job_title = models.CharField(...) But, if I have created an organisation model, and will let multiple users belong to a certain organization, then how to modify the Django User model to that one organization has multiple users? -
Django in docker. Can't see logs
I am trying to pack a django project into docker environment. My docker file looks like FROM python:2.7 EXPOSE 8000 COPY requirements.txt /tmp/requirements.txt RUN pip install -r /tmp/requirements.txt COPY code /code WORKDIR /code ENTRYPOINT ./manage.py runserver 0.0.0.0:8000 requirements.txt Django==1.10.6 Everything works fine but id don't see any output from django server. If I am attaching to container then I can see its output. -
Best practise to remove stale documents in elasticsearch
I have a django app that pushes models into elasticsearch. I have a post signal to update after save but want to write a batch command that is updating all documents. Within this process I want to remove documents that became stale (e.g. set inactive, got deleted etc in the database). I started with something like this: update all documents and store the updated / created ids. create one gigantic exclude-query delete all documents that are matching Something like this: for i in updated_ids: q = Q('match', **{'id': i}) f = f | q if f else q queryset = dt.search().query(Bool(filter=[~Q(f)])) for stale in queryset.scan(): stale.delete() But the query becomes to long and that fails. I wonder if there is a more efficient way of doing this. I use elasticsearch-dsl upon elasticsearch.py. Django-Haystack is not an option. -
Django project without storing data in database
I wrote a script to collect some informations about products offerred by few eShops. This script runs once an hour and collects data to csv file. File is overwritten everytime so it contains currently offered products only. Csv file contains fields - shop name, price, vendor, description, size, picture URL, product page URL and looks like this (just two lines of about 1500 lines) sklep-presto.pl, 149.90, real, Deck Davis Northern Light 8,38 Real, 8.38, https://www.sklep-presto.pl/pol_il_Deck-Davis-Northern-Light-8-38-Real-59111.jpg, https://www.sklep-presto.pl/product-pol-59111-Deck-Davis-Northern-Light-8-38-Real.html sklep-presto.pl, 169.90, real, Deck Embossed Elite Ferguson 8,5 Real, 8.5, https://www.sklep-presto.pl/pol_il_Deck-Embossed-Elite-Ferguson-8-5-Real-56151.jpg, https://www.sklep-presto.pl/product-pol-56151-Deck-Embossed-Elite-Ferguson-8-5-Real.html This script is also generating dictionary csv files - price, vendor, size. These files contain unique values from particular field form main csv file. For example vendor dictionary loks like this almost,blind,chocolate,deathwish,foundation,goodwood,machine,real,zero 3,3,4,2,3,2,2,9,7 It contains vendor names and occurence count (in main csv). I would like to make a web page using django but I have never been using django. I would like to make a web page that will present 5 choice lists and search button on top. Choice lists will be vendor, from price, to price, from size, to size. I would like possible choices to be imported (from csv files) to these choise lists on web page every time … -
Django Queryset distinct and order by multi times
I want distinct article then ordering(multi fields), the queryset always resuable. The below code queryset = queryset.distinct('article_hash').order_by('article_hash') queryset = querset.order_by('create_time') SQL Error SELECT DISTINCT ON expressions must match initial ORDER BY expressions The below code lost `distinct article_hash will not work as expect. queryset = queryset.distinct('article_hash','create_time') .order_by('article_hash', 'create_time') SQL Like select distinct on (tb.article_hash, tb.create_time), tb.id from tb order by tb.article_hash asc, tb.create_time asc In django document some link but not clear. https://code.djangoproject.com/ticket/24218 -
Mongoengine creating list field inside dictionary field using embeeded document
I am using django 1.10 and mongodb in backend. For mongodb I am using mongoengine 0.11. I have following models defined in models.py class embed(EmbeddedDocument): fruits = ListField(required = True) chocolates = ListField(required = True) class test_coll(Document): id = StringField(primary_key = True) test1 = MapField(EmbeddedDocumentField(embed)) test2 = MapField(EmbeddedDocumentField(embed)) my views.py contain method as- def addDetail(request): fruit = ['mango','orange'] c = ['kitkat','eclairs'] data = test_coll(id='one') eObj = embed(test1=fruit,test2=c) data.test1.value = eObj data.save() This code storing data in mongodb as- { "_id": "role", "test1": {}, "test2": {} } And my expected output is as- { "_id": "role", "test1": { 'fruits' = ['mango', 'orange'], 'c' = ['kitkat', 'eclairs'] } "test2": {}, } Here, what I am trying to do is storing list as dictionary value. I am new to mongodb and mongoengine. Can you suggest solution to above problem or give better solution. At the end I am expecting output list when I query, test1.fruits = ['mango', 'orange'] -
how add integer properties from a foreignkey objects set in django template?
in my models, each product has variations, and each variation has orderitem , each orderitem has integer value which is quantity is there way in django template to add all of the quantities of a particular product and display it ? class Product(models.Model): name = models.CharField(max_length=255) price = models.DecimalField(decimal_places=2, max_digits=12) store = models.ForeignKey(Store) class Variation(models.Model): product = models.ForeignKey(Product) title = models.CharField(max_length=255) active = models.BooleanField(default=True) stock = models.PositiveIntegerField(null=True, blank=True) deleted = models.BooleanField(default=False) class Orderitem(models.Model): variation = models.ForeignKey(Variation) quantity = models.PositiveIntegerField() <-- i want to display the sum of this order = models.ForeignKey(Order) -
Scrapy cannot find Django app settings
My localhost is worked, but production host cannot.Who can help me this? File "/usr/bin/scrapy", line 11, in <module> sys.exit(execute()) File "/usr/lib64/python2.7/site-packages/scrapy/cmdline.py", line 108, in execute settings = get_project_settings() File "/usr/lib64/python2.7/site-packages/scrapy/utils/project.py", line 68, in get_project_settings settings.setmodule(settings_module_path, priority='project') File "/usr/lib64/python2.7/site-packages/scrapy/settings/__init__.py", line 292, in setmodule module = import_module(module) File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module __import__(name) File "/usr/lib/python2.7/site-packages/project-1.0-py2.7.egg/Crawler/settings.py", line 20, in <module> application = get_wsgi_application() File "/usr/lib64/python2.7/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application django.setup(set_prefix=False) File "/usr/lib64/python2.7/site-packages/django/__init__.py", line 22, in setup configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) File "/usr/lib64/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__ self._setup(name) File "/usr/lib64/python2.7/site-packages/django/conf/__init__.py", line 41, in _setup self._wrapped = Settings(settings_module) File "/usr/lib64/python2.7/site-packages/django/conf/__init__.py", line 97, in __init__ mod = importlib.import_module(self.SETTINGS_MODULE) File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module __import__(name) ImportError: No module named Dashboard.settings. Dashboard is a django project -
How do I display the console output to HTML in Django?
So I'm using Django framework to display the console output to the HTML. To execute the command, I'm using the check_output of subprocess in Python. Which receives the input from the HTML input form. Below is the code of the views file and HTML file. I'm a novice in this so I'd appreciate your assistance. Views.py from django.shortcuts import render from django.shortcuts import redirect from .forms import command_form import subprocess as sp # Create your views here. def welcome_page(request): output="" if request.method == "POST": myform = command_form(request.POST) if (myform.is_valid()): execute_command = myform.cleaned_data['cmd_string'] output = sp.check_output(execute_command, shell=True) else: myform = command_form() return render(request, 'ovs/welcome.html', {'output': output}) else: return render(request, 'ovs/welcome.html', {}) welcome.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>welcome to ovs GUI</title> </head> <body> <h3>Choose the option:</h3> <form method="POST">{% csrf_token %} Enter the command: <input type="text" name="cmd_string" id="cmd_string"/> <input type="submit" value="Run"/> </form> <h3>{{ output }}</h3> </body> </html> -
How to control task runner with web server
I write one django project: one part is config server, It can config new task by web user, and store into database with django server. Other part is task runner(pull task from database and execute these tasks), at the beginning, I write this part into djang's management command, for example, management/run_task.py: class Command(BaseCommand): help = 'run task' def handle(self, *args, **options): # pull task and run, in fact, here I use subprocess for control while True: if global_signal_running: for task in pull_task(): task.run() But now, I want to control it (start, stop, running) in config web front page, should I write a new server for task runner (accept user control, start new process(task runner), or stop the process). But how to write two server for one django project, or you have more better solution for this. -
Deploying a Flask/Django app with AWS OpsWorks
Right, so I am very new to AWS OpsWorks, and when I try and install a vanilla python app via a custom cookbook, it gives me this error: "No resource or method named ensure_scm_package_installed" I have tried using most of the custom cookbooks available through Google, like https://github.com/alecpm/opsworks-web-python. Could anyone help with this? Thank you! -
Templates does no exist for rest_framework/API.html
I am going through the tutorial of Django rest framework and API format is not working. The json format is working though. The error is templates does not exist rest_framework/API I am using python3.4 and Django 1.10.6 The rest framework is listed within my installed apps in the settings.py just like described in the docs The template loader postmortem does not provide the list of tried templates -
Serialize with PK and deserialize with field data in django-rest-framework
I have the following two serializers for the models Person and Title in my application, class TitleSerializer(serializers.ModelSerializer): class Meta: model = Title exclude = ['created', 'modified'] class PatientSerializer(serializers.HyperlinkedModelSerializer): title = serializers.PrimaryKeyRelatedField(queryset=Title.objects.all()) class Meta: model = Patient exclude = ['created', 'modified', 'owner'] My requirement is, I should be able to do a successful, POST with a Person payload and with the Primary Key for Title. For example, { "title": 5, "name": "Sample Name", "age": "18" } Here 5 (for title) is the Primary Key of Title. But when I do a GET, I need to get the actual data for title, for example, { "title": "Mr.", "name": "Sample Name", "age": "18" } Is this requirement possible to achieve, if so what would be the best approach to follow? -
Writing tests for a Django view that uses Raw SQL
I am building a test suite for a django/DRF application using --keepdb and the 'django.db.backends.sqlite3' backend for all of the databases (this project has multiple). I have run into a situation where the django ORM is not being used - a non-trivial SQL query is being run using .raw. This particular query is Postgresql specific. The problem is that it fails against the sqlite3 backend - but there are almost 150+ tests on this database (and more to be created) and switching the entire thing to Postgres will significantly increase the run time for it. As I see it we only have a few not so great options: Hope there is an elegant solution that someone might be able to share with us Run this database against a local postgres server and eat the run time on the tests Use what I can only image is a hacky solution involving using environment or system variables to try and route this test class to use a separate postgres backend Simply create a new/parallel CI pipeline where this case and any future cases like it are run against a postgres backend and ignored on the sqllite backend (and normal ORM tests ignore … -
How to properly clone table tr?
I am working on a Django project i have a form in there is table <table name="mytable" id="table_purchase" role="grid"> <thead> <tr> <th class="text-center" hidden>No</th> <th class="text-center" hidden>Part Id</th> <th class="text-center">Manufacturer *</th> <th class="text-center">Phone Model *</th> <th class="text-center">Part Name *</th> <th class="text-center">Quantity *</th> <th class="text-center">Price *</th> </tr> </thead> <tbody> <tr id = "gonnaclone"> <td> {% if count_phonemaster %} <div class="col-md-12 column"> <select class='form-control' name="phoneManufacturer" id='id_manufacturer' onchange="selected_manufacturer(this)" required> <option value="" selected disabled>Please select</option> {% for phoneManufacturerObject in phoneManufacturer %} <option>{{ phoneManufacturerObject.phoneManufacturer }}</option> {% endfor %} </select> </div> {% else %} <div class="col-md-12 column"> <select class='form-control' name="phoneManufacturer" id='id_nomanufacturer' data-toggle="modal" data-target="#no_manufacturer" required> <option value="" selected disabled>Please select</option> </select> </div> {% endif %} </td> <td> <div class="col-md-12 column"> <select class='form-control' name="modelName" id='id_model' onchange="selected_model(this)" required> <option value="" selected disabled>Please select</option> </select> </div> </td> <td> <div class="col-md-12 column"> <select class='form-control' name="partName" id='id_part' required> <option value="" selected disabled>Please select</option> </select> </div> </td> <td> <input type="text" name='quantity' class="form-control" pattern="^[0-9]{1,}$" maxlength="11" required/> </td> <td> <input type="text" name='costPerUnit' class="form-control" pattern="^[0-9]{1,}$" maxlength="11" required/> </td> </tr> </tbody> <a id="add_row" class="btn btn-primary btn-md" onclick="add()" style="padding:10px 60px">Add Row</a> <a id="remove_row" class="btn btn-default btn-md" onclick="remove()" style="padding:10px 47px;">Remove Row</a> <button id="button1id" class="btn btn-primary btn-md" type="submit" style="padding:10px 66px;">Submit</button> <a href="/" class="btn btn-success btn-md" type="reset" style="padding:10px 67px; background-color:#95a5a6; border-color:#95a5a6;">Cancel</a> …