Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Ignoring file in Github and Django
Me and my friend are both working on a project in Django. We are beginners with both GitHub as well as Django. We have managed to use .gitignore to ingore certain files. This is how the .gitignore looks like : .DS_Store groupProject/__pycache__ marketplace/__pycache__ marketplace/migrations/__pycache__ marketplace/templatetags/__pycache__ db.sqlite3 The only issue that we have is that when we try to push we get an error saying that there is a merge conflict for this file 'db.sqlite3' . I have added this file in .gitignore and for some reason it still decides to read it? What should we do? -
How to overwrite a file with same name when uploaded to Django FileField?
I want to overwrite the contents of a file if the filename is the same when an user uploads a file in Django. I have gone through many stackoveflow questions but haven't found what I wanted. Following is my model: class Upload(models.Model): profile = models.FileField(upload_to="uploads/") Code to upload files: files = {'newprofile': open('profiles/blah.jmx', 'rb')} x = requests.post(url, files=files) Any pointers would be appreciated. -
Query Django JSONFields that are a list of dictionaries
Given a Django JSONField that is structured as a list of dictionaries: # JSONField "materials" on MyModel: [ {"some_id": 123, "someprop": "foo"}, {"some_id": 456, "someprop": "bar"}, {"some_id": 789, "someprop": "baz"}, ] and given a list of values to look for: myids = [123, 789] I want to query for all MyModel instances that have a matching some_id anywhere in those lists of dictionaries. I can do this to search in dictionaries one at a time: # Search inside the third dictionary in each list: MyModel.objects.filter(materials__2__some_id__in=myids) But I can't seem to construct a query to search in all dictionaries at once. Is this possible? -
Programatically create nested plugins to display data on Django CMS
I have been trying to retrieve multiple arrays of data from firebase, turn the data into multiple plugins and then pass those into one of my templates. I have been trying to find information on the docs about it but all I can find is this. As well as another individual posted a similar question with a link to a blog but given my little experience with Django CMS I am unable to determine if I have a similar scenario. The code that I have so far is the following. Any guidance would be greatly appreciated, thanks! cms_plugins.py from cms.plugin_base import CMSPluginBase from cms.plugin_pool import plugin_pool from django.utils.translation import ugettext_lazy as _ from . import models class FeaturedBoxPlugin(CMSPluginBase): model = models.FeaturedBox module = _('My Project') name = 'Featured Box' render_template = 'featured_box.html' allow_children = False class BoxPlugin(CMSPluginBase): model = models.Box module = _('My Project') name = 'Box' render_template = 'box.html' allow_children = False plugin_pool.register_plugin(FeaturedBoxPlugin) plugin_pool.register_plugin(BoxPlugin) models.py from django.utils.encoding import python_2_unicode_compatible from cms.models import CMSPlugin @python_2_unicode_compatible class FeaturedBox(CMSPlugin): title = models.CharField( blank=True, max_length=200, ) image_id = models.CharField( blank=True, max_length=200, ) books = models.CharField( blank=True, max_length=200, ) author = models.CharField( blank=True, max_length=200, ) date = models.CharField( blank=True, max_length=200, ) @python_2_unicode_compatible class Box(CMSPlugin): … -
App does not include some of the files on Heroku
Everything works fine in my local development environment. I don't get a Chrom-Dev console error, but on Heroku my app doesn't find some files. I am using Whitenoise and in my template these files are defined as static: Local dev environment Heroku app This function is defined as follows: async function run() { // load the models await faceapi.loadMtcnnModel(`{% static 'models/mtcnn/mtcnn_model-weights_manifest.json' %}`); await faceapi.loadFaceRecognitionModel(`{% static 'models/fr/face_recognition_model-weights_manifest.json' %}`); await faceapi.loadTinyFaceDetectorModel(`{% static 'models/fd/tiny_face_detector_model-weights_manifest.json' %}`); navigator.mediaDevices.getUserMedia({audio: false, video: true}).then(stream => { video.srcObject = stream; video.play(); }).catch(e => console.warn(e)); } The models are in the following location: What exactly should I configure, can I tell Whitenoice to include these files in the collectstatic command? Or am I missing something else? I don't even see the Models folder in the Chrome Dev Tool in the Source tab. Even in the local development environment, although on the local development all work, but not on Heroku. Link to the heroku app: https://gibbface.herokuapp.com/app/ Link to the GitHub repo: https://github.com/khashashin/gibbface -
Adding a separate field to a form in django template in view
So this is what I want to do: I have a model, that I created a form to fill using forms.Form. But one of the variables is a string but is entered in 3 separate fields in HTML, then concatenated in the view. To simplify the question, I'm gonna post this example model: class Example(models.Model): var1 = models.CharField(max_length=10, default="XXXX-A-1") var2 = models.CharField(max_length=15) var3 = models.IntegerField() str bla bla bla And this is the example form: class ExampleForm(forms.Form): var2 = forms.CharField(max_length=10) var3 = forms.IntegerField() def clean(self): cleaned_data = super(ExampleForm, self).clean() var2 = cleaned_data.get('var2') var3 = cleaned_data.get('var3') if not var2 or not var3: raise forms.ValidationError('Please fill all fields') And this is the template: <form enctype="multipart/form-data" id="regForm" method='POST'> {% csrf_token %} <div class="row"> <div class="col-sm"> <div class="tab"> <label for="formGroupExampleInput">Please Enter Var1</label> <div class="row"> <div class="col"> <input type="text" class="form-control" placeholder="Part 1 of var1" name="var1-part1"> </div> <div class="col"> <select class="custom-select" name="var1-part2"> <option value="A">A</option> <option value="B">B</option> <option value="D">D</option> </select> </div> <div class="col"> <input type="text" class="form-control" placeholder="Part 3 of var1" required="false" name="var1-part3"> </div> </div> {{ form|crispy }} </div> </div> </div> </form> Finally this is the view: @login_required def exampleview(request): var1_part1 = request.POST.get('var1-part1', False) var1_part2 = request.POST.get('var1-part2', False) var1_part3 = request.POST.get('var1-part3', False) var1 = var1-part1 + '-' + … -
Django Queryset - rename original values and re-use original value names
I'm completing a challenge for a job and I'm a little confused with this endpoint's response and I'm running out of time (had almost no internet connectivity for a week and the deadline is tomorrow). I have the following models: Attribute AttributeValue ProductAttribute I need to get all attributes that are linked to a given product ID. I have managed to get the values but I can't give them the correct names in the response. This is what I'm trying to do (code in get_attributes_from_product function): src/api/viewsets/attribute.py from django.db.models import F from rest_framework import viewsets from rest_framework.decorators import action from rest_framework.response import Response from api import errors from api.models import Attribute, AttributeValue, ProductAttribute from api.serializers import AttributeSerializer, AttributeValueSerializer, AttributeValueExtendedSerializer import logging logger = logging.getLogger(__name__) class AttributeViewSet(viewsets.ReadOnlyModelViewSet): """ list: Return a list of attributes retrieve: Return a attribute by ID. """ queryset = Attribute.objects.all() serializer_class = AttributeSerializer @action(detail=False, url_path='values/<int:attribute_id>') def get_values_from_attribute(self, request, *args, **kwargs): """ Get Values Attribute from Attribute ID """ attribute_id = int(kwargs['attribute_id']) # Filter queryset to find all values for attribute response = AttributeValue.objects.filter(attribute_id=attribute_id).values( 'attribute_value_id', 'value') # Return response if response.exists(): return Response(response, 200) else: return Response(response, 204) @action(detail=False, url_path='inProduct/<int:product_id>') def get_attributes_from_product(self, request, *args, **kwargs): """ Get all … -
How to migrate existing models to Wagtail Page models
I have a existed model,which is EmsanWorks(models.Model). I would like to have a same page model, so I copy a new model and named it EmsanWorksPage and changed models.Model to Page. class EmsanWorksPage(Page): id_works = models.AutoField(primary_key=True) name_r = models.CharField(max_length=255) name_o = models.CharField(max_length=255) title_r = models.TextField() title_o = models.TextField() title_t = models.TextField() birth_y = models.CharField(max_length=20) birth_c = models.ForeignKey(EmsanPays, models.DO_NOTHING, db_column='birth_c',related_name='page_emsanpays_birth_c') gender = models.CharField(max_length=6) dest = models.CharField(max_length=255) media_spec = models.CharField(max_length=255) year = models.TextField() # This field type is a guess. commission = models.CharField(max_length=255) performer = models.CharField(max_length=255) first_perf = models.CharField(max_length=255) duration = models.CharField(max_length=255) perf_c = models.ForeignKey(EmsanPays, models.DO_NOTHING, db_column='perf_c') context = models.TextField() instr = models.TextField() cycle = models.CharField(max_length=255) media_w = models.CharField(max_length=255) setup = models.CharField(max_length=255) prod_loc = models.TextField() prod_per = models.TextField() perf_tech = models.TextField() perf_media = models.TextField() publisher = models.CharField(max_length=255) audio = models.CharField(max_length=255) prog_notes = models.TextField() reception = models.TextField() editor = models.CharField(max_length=255) phono = models.CharField(max_length=255) comment = models.TextField() timestamp = models.DateTimeField() modif = models.TextField() afficher = models.CharField(max_length=3) restricted_editors = models.TextField() class Meta: managed = False db_table = 'emsan_works' However, after migrate I got this error OperationalError at /admin/emsanapp/emsanworkspage/ (1054, "Unknown column 'emsan_works.page_ptr_id' in 'field list'") -
Django - How to store and render a Pandas dataframe/CSV for each model entry?
I have a model that represents a single patient. It stores information such as the patient_name (CharField), weight (IntegerField), height (IntegerField) etc. I extended the save method save(self, *args, **kwargs) to perform a series of calculations/functions (e.g. calculate BMI) to save back to the model. For the extended save() method, it also runs a function that calculates weekly weight loss goals in pandas and saves to CSV (named by patient_name) in a media folder. I'm trying to figure out the best way to display that CSV data back for each patient. Currently, I'm using the DetailView CBV to serve up a detail page for each patient with their new computed model fields (e.g. <p>{{ object.patient_name }}'s calculated BMI is {{ object.bmi }}</p>) There are three ideas I have but I'm not sure if they will work or how to implement it. 1 - I could generate an HTML table instead of CSV, is there a way to render custom HTML snippets from a media folder based on a trait of the model or primarykey id {% media|object.patient_name %}? 2 - I could try to save the pd.DataFrame, CSV (or the string text representation of an HTML table) to a new … -
How to create search box in Django. it is for our project. ooooooooooooooooooooooooooooooooooo
I want to use the search box in Django to our website project. could you write step by step? what do I write on my HTML, views, URL, models etc? -
Django template iframe card information
I have a payment gateway that returns redirect url for getting card information when supplied with right authentication credentials. Now I am comparing two possibilities for displaying this url to users: Redirecting user to given url Loading content of redirect url in django template as iframe From design and end user's aspects, it is obviously better to include content as iframe. However, I suppose that there is security concerns in this option. Is it secure to load such content as iframe in django template? Which measures should be taken to avoid possible security problems? Note: Payment processor didn't provide any clause about iframe in its documentation. -
Display Django Field properties, such as min_value and max_value, in the Template
I have a Django Form with an IntegerField. I give the IntegerField a min_value and a max_value. I would like to DISPLAY that min_value and max_value for the user, so I'm trying to include it in the template next to the input field itself. Seems like this should not be so hard, but it isn't working. :-( Below, the "print" statement in views.py works just fine. So I know that one can access the min_value from the field with just ".min_value". But the min_value in the template does not show up in the browser. Thoughts anyone?? In forms.py class MyForm(forms.Form): somenumber = forms.IntegerField(min_value=0, max_value=12) In views.py: form = forms.MyForm() print(form.fields['somenumber'].min_value) ... context = {'form':form} ... In template: {{ form.somenumber.min_value }} -
Django test pagination EmptyPage
I am trying to write a function that tests if we exceed the page limit it returns the last page, but I'm a little stuck Here is my function: def test_pagination_returns_last_page_if_page_out_of_range(self): response = self.client.get(reverse('catalog:search'), {'query': '', 'page': 999}) # Check that if page is out of range (e.g. 999), deliver last page of results self.assertEquals(response.context['products'], response.context['products'].paginator.page(2)) When I run this test it returns to me: Traceback (most recent call last): File "/home/rouizi/pur_beurre/catalog/tests/test_views.py", line 120, in test_pagination_returns_last_page_if_page_out_of_range self.assertEquals(response.context['products'],response.context['products'].paginator.page(2)) AssertionError: <Page 2 of 2> != <Page 2 of 2> my view function is somthing like this: ... try: prods = paginator.page(page) except EmptyPage: # We return the last page prods = paginator.page(paginator.num_pages) return render(request, 'catalog/search.html', {'products': prods, 'query': query}) -
where Environment variables for python are saved
I know to set an environment variable in python is to use os.environ['API_USER'] but where this variable is saved, I supposed this environment variable is saved in .env file but it wasn't. on the console to retrieve all the environment variables use command: os.environ but don't know where are saved. need your help, Thanks! -
How to render a name after clicking/tabbing out of a field in Django?
I have a form that is used to keep track of when employees enter/leave an area to track times. The first field is where they enter their employee #. I have a table in my database that comes from another model, called Salesman, where all the employee data (names, dept., etc.) is stored, as shown here (only fields relevant to question shown): alldata/models.py class Salesman(models.Model): slsmn_name = models.CharField(max_length=25) id = models.IntegerField(db_column='number', primary_key=True) ... def __str__(self): return "" My model for the form is set so that the employee_number field is related to Salesman, so that the numbers entered in my form can only be numbers that are valid in Salesman based on the id and so that I can filter "who" can submit the form or not (depending on team/status). models.py class EmployeeWorkAreaLog(TimeStampedModel, SoftDeleteModel, models.Model): employee_number = models.ForeignKey(Salesman, on_delete=models.SET_NULL, help_text="Employee #", null=True, blank=False) ... forms.py class WarehouseForm(AppsModelForm): class Meta: model = EmployeeWorkAreaLog widgets = { 'employee_number': ForeignKeyRawIdWidget(EmployeeWorkAreaLog._meta.get_field('employee_number').remote_field, site, attrs={'id':'employee_number_field'}), } fields = ('employee_number', 'work_area', 'station_number') def clean_employee_number(self): employee_number = self.cleaned_data.get('employee_number') if employee_number is None: raise forms.ValidationError("Must enter emp #") elif employee_number.team not in ('WF', 'WP', 'OM') or employee_number.employee_status not in 'A': raise forms.ValidationError("Employee not valid, please contact manager") return employee_number … -
Django: Way to Get List of All Running Commands?
If I run htop, I see a list of all running processes, and the commands that spawned them. Is there a way to get the list of running commands from inside Django? I looked at the docs for the OS module, but didn't yet spot a way to do it. -
Django Admin: Custom ordering according to concatenated charfields of a related manytomany model
Here are my simplified models: class MasterFood(models.Model): class Meta: verbose_name_plural = 'Master Foods' FOOD_TYPE_CHOICES = ( ('F', 'Fats'), ('C', 'Carbohydrates'), ('P', 'Proteins'), ('O', 'Others'), ) food_name = models.CharField(max_length=255) food_type = models.CharField(max_length=20,choices=FOOD_TYPE_CHOICES) def __str__(self): return self.food_name class MasterMeal(models.Model): class Meta: verbose_name_plural = 'Master Meal Plan Meals' proteins = models.ManyToManyField(to="MasterFood", limit_choices_to={'food_type': 'P'},blank=True,related_name='food_proteins') carbohydrates = models.ManyToManyField(to="MasterFood", limit_choices_to={'food_type': 'C'}, blank=True, related_name='food_carbohydrates') fats = models.ManyToManyField(to="MasterFood", limit_choices_to={'food_type': 'F'}, blank=True, related_name='food_fats') def all_foods(self): return list(self.proteins.all())+list(self.carbohydrates.all())+list(self.fats.all()) def __str__(self): return ', '.join(map(lambda x: x.food_name, self.all_foods()))+f' ({len(self.all_foods())})' and my modelAdmin object in admin.py: class MealAdmin(admin.ModelAdmin): model = MasterMeal save_as = True ordering = ('proteins__food_name','carbohydrates__food_name','fats__food_name',) What I would like is in the Django admin page for MasterMeals is to have each object named after the __str__ method given in my MasterMeal model, but in a different ordering than the default. Specifically, I would like the objects to be sorted in alphabetical order according to the __str__ method definition, and if possible, I don't want to modify my MasterMeal model to add another field to achieve this. I have tried several things such as the ordering = ('proteins__food_name','carbohydrates__food_name','fats__food_name',) in my MealAdmin object as well as ordering = ('proteins','carbohydrates','fats',). I have also tried overwriting the queryset of the ModelAdmin with an annotate function … -
How to update the field of django model from the existing field values of object
How to update the field of django model by taking/extracting values or filtering values from existing field of same object? I have the following model: from django.contrib.postgres.fields import JSONField,ArrayField class MyUrlModel(models.Model): urls_json_data = JSONField(blank=True,null=True,default=dict) urls_list = ArrayField(models.TextField(blank=True),blank=True,null=True,default=list) field urls_json_data has the following data in json: [{"protocol":"http","hostname":"google.com","port":80"},{"protocol":"https","hostname":"apple.com","port":443"}] Here, i would like to fetch the structures from urls_json_data and make it into url structure like following and save them under urls_list which is ArrayField. urls_list=['http://google.com:80','https://apple.com:443'] So whenever urls_json_data get's updated, I want to fetch the URLs from it and save it under urls_list on the fly. What is the best approach to do it, is it by signals or by altering save method? urls_list = [] for each_data in urls_json_data: url = each_data['protocol']+each_data['hostname']+each_data['port'] urls_list.append(url) -
How can I change in db values of model object by celery worker with redis in Django?
I want to create an app with workers which in the background changes the value of Simulation model. Firstly user clicks to start simulation, wait some time, click to end simulation, and then on the page shows a value difference (from the start to the end of simulation) which was changed by the background worker. The main problem that I just can't reach the worker's task. I can reach @receiver after Simulation model saved and correctly check model`s values but I cant change model values in db by workers. The project called app, application - myapp. app/myapp/models.py from django.db import models class Simulation(models.Model): # changed every simul day created_date = models.DateField(auto_now_add=True) today = models.DateField(auto_now=False, auto_now_add=False) status = models.BooleanField(default=False) daemon_active = models.BooleanField(default=False) info = models.TextField() def get_simulation_day(self): """days passed after simulation firstly start""" # sim = Simulation.objects.all().last() return (self.today - self.created_date).days app/myapp/views.py def simulate(request, action): sim = get_simulation() if action == "disable" and sim.status == True: sim.status = False sim.save() elif action == "enable" and sim.status == False: print('view simulate ok') sim.status = True sim.save() print('view simulate ok after save') context = { "simulations_exists": True, "days_passed": sim.get_simulation_day(), "simulation_today_str": (sim.today).strftime("%d %B, %Y"), "simulation_status": sim.status } return render(request, "simulation_page.html", context=context) app/myapp/tasks.py import logging … -
How do you test django-axes with a Django unit test?
I'm using Django Axes with my Django project to ensure that if someone is trying to guess a password, their IP gets blocked for a while. It's setup, and works great. I want to write a Django test that makes sure that after X number of guesses, the user is really locked out. The problem I'm having is that if you try to use the django test client to do a login as you normally would: self.client.login(username="invalid_user", password="invalid_password") Axes crashes, because it isn't getting a request arg: Traceback (most recent call last): File "/opt/my_project/website/login_app/tests/test_views.py", line 71, in test_brute_force_attempts self.client.login(username="invalid_user", password="invalid_password") File "/opt/my_project/venv3/lib/python3.6/site-packages/django/test/client.py", line 602, in login user = authenticate(**credentials) File "/opt/my_project/venv3/lib/python3.6/site-packages/django/contrib/auth/__init__.py", line 73, in authenticate user = backend.authenticate(request, **credentials) File "/opt/my_project/venv3/lib/python3.6/site-packages/axes/helpers.py", line 411, in inner return func(*args, **kwargs) File "/opt/my_project/venv3/lib/python3.6/site-packages/axes/backends.py", line 39, in authenticate "AxesBackend requires a request as an argument to authenticate" axes.exceptions.AxesBackendRequestParameterRequired: AxesBackend requires a request as an argument to authenticate This is a known issue, and is discussed here: https://github.com/jazzband/django-axes/issues/433 As far as I can tell, you just need to pass self.client.login a request= arg, which it will pass to django.contrib.auth.authenticate, and it'll be happy. I can't figure out where to get this request within a test … -
Can I make a select type="file"?
Ok, So I need to implement a drop-down where I can choose some file from a folder directory. I already have the dropdown displaying the files but when I'm making the query I'm getting this error django.utils.datastructures.MultiValueDictKeyError:'xxxxxx' I'm not sure if is even passing the whole file or just the name of the file. Can I do something as a <select type="file">? or can I connect an hidden input type="file" to the select? Thanks -
Intermittent hang using pandas read_parquet inside django api wsgi thread
I'm writing an API using Django 2.2, djangorestframework 3.9, and pandas 0.25. I use AWS Elastic Beanstalk to deploy under Amazon linux 2.8 using python 3.6. The application uses pandas DataFrame objects stored as .parquet files in an s3 bucket. In the python shell I can call my custom properties, for example data_file.row_count should read in the parquet file as a pandas DataFrame (using pandas.read_parquet('s3://bucket-name/file-name')), and simply return the number of rows in the file. This has always worked without issue via the python shell, both locally and remotely. The problem happens when that read_parquet method runs as a result of a web request on the ec2 instance via wsgi. I want the application to simply respond to a web request with the row_count of a particular data_file. Right now, it only works about half the time, and the other half it hangs for about 5 minutes and gives me a generic 500 error with no stacktrace (Yes DEBUG==True). I tried a workaround where I downloaded the s3 file to a tmp local file, then ran pandas.read_parquet('TMP_FILE.parquet'), and the hang happened on the read_parquet not on the s3 download, so I ruled out any network issues between my ec2 instance … -
In Django models, can I fetch the value of a field, process it and store it in another field?
I want to reference the value of a field in another field. I've tried default=self.key_name * 2, but that won't work. class Pizza_size(models.Model): length = models.DecimalField(decimal_places=2, default=0) price = models.DecimalField(decimal_places=2, default=self.length*2) When I create an object with the Pizza_size class, if the value of length is 5, I want the value of price to be 10 by just running the command Pizza_size(length=5). -
How can I organize a database with products and users?
I am currently trying to organize a django database model for an online shop-system with users and products. My code: class UserData(models.Model): username = models.CharField(max_length=100) password = models.CharField(max_length=500) bought_products = models.ForeignKey(MarketProducts, on_delete=models.CASCADE) class VendorData(models.Model): username = models.CharField(max_length=100) password = models.CharField(max_length=500) sold_products = models.ForeignKey(MarketProducts, on_delete=models.CASCADE) class MarketProducts(models.Model): category = models.CharField(max_length=100) vendor = models.ForeignKey(VendorData, on_delete=models.CASCADE) name = models.CharField(max_length=200) description = models.CharField(max_length=1000) price = models.IntegerField() pub_date = models.DateTimeField('Date published') image = models.ImageField(upload_to=b'shop/media/images/') likes = models.IntegerField() dislikes = models.IntegerField() How can I organize a good working system so all the products a user bought are saved inside the bought_products column and all the products a vendor sold can be saved inside the sold_products column. Do I have to use a ForeignKey for that or is there something more suitable for this situation? Also, if there is anything unwise about the existing structure of the database model (for example the current image field column only saves the link but not the image itself which is kinda weird...), please feel free to correct me :). Many thanks in advance :D -
How to use HTTP headers to track number of subscribers?
I am trying to implement a web based feed reader using Django by implementing the package django-feed-reader (https://pypi.org/project/django-feed-reader/). But in the project description the author mentioned like this: Tracking subscribers and read/unread state of feeds The app does not (currently) track the read/unread state of posts within a feed. That will need doing in your project according to your needs. The app assumes that each feed only has one subscriber that is the project itself. If your project can allow personal subscriptions for individual users, you can let the app know on per feed basis how many subscribers it has by settings num_subs on a Source object. The app will then report this via the user agent to the feed source for analytics purposes. I have no clue what does the last lines means and how to implement the same. Please help me resolve this. (My application need to avail authenticated users to subscribe feeds they wanted.) The structure of the 'Source' model is as follows: class Source(models.Model): # This is an actual feed that we poll name = models.CharField(max_length=255, blank=True, null=True) site_url = models.CharField(max_length=255, blank=True, null=True) feed_url = models.CharField(max_length=255) image_url = models.CharField(max_length=255, blank=True, null=True) description = models.TextField(null=True, blank=True) last_polled …