Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
In Django, how to utilise extra fields in queries
In Django, I have a following query: measurements = Measurement.objects.all().extra({'value_float': "CAST(value as NUMERIC)"}).aggregate(Avg('value_float')) Basically, I have some data in the database where the value field is varchar instead of a number. So I try to cast it to numeric type and then get average of all values of returned objects. However, this fails to a following error: django.core.exceptions.FieldError: Cannot resolve keyword 'value_float' into field. Choices are: channel_id, created, id, unit, value Is there a way to reference to the extra field I created in the query? -
How to access fields in model in view?
Hi I am new to Django and web development. I want to use the data in my model in order to do some calculations in views.py. What is the syntax required in views to access the data fields which is in the model? -
django project with heroku: application error
I successfully deploy the project to Heroku. but when I run Heroku open it throws Application error An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details. my logs is here (dj12) λ heroku logs 2018-07-05T07:06:17.443987+00:00 app[api]: Initial release by user ermiaskidanegebre@gmail.com 2018-07-05T07:06:17.443987+00:00 app[api]: Release v1 created by user ermiaskidanegebre@gmail.com 2018-07-05T07:06:17.909289+00:00 app[api]: Release v2 created by user ermiaskidanegebre@gmail.com 2018-07-05T07:06:17.909289+00:00 app[api]: Enable Logplex by user ermiaskidanegebre@gmail.com 2018-07-05T07:16:35.051508+00:00 app[api]: Attach DATABASE (@ref:postgresql-shaped-50056) by user ermiaskidanegebre@gmail.com 2018-07-05T07:16:35.051508+00:00 app[api]: Release v3 created by user ermiaskidanegebre@gmail.com 2018-07-05T07:24:48.143580+00:00 app[api]: Set SECRET_KEY config vars by user ermiaskidanegebre@gmail.com 2018-07-05T07:24:48.143580+00:00 app[api]: Release v4 created by user ermiaskidanegebre@gmail.com 2018-07-05T07:33:04.050618+00:00 app[api]: Set SECRET_KEY config vars by user ermiaskidanegebre@gmail.com 2018-07-05T07:33:04.050618+00:00 app[api]: Release v5 created by user ermiaskidanegebre@gmail.com 2018-07-05T07:40:34.881810+00:00 app[api]: Set EMAIL_PASSWORD config vars by user ermiaskidanegebre@gmail.com 2018-07-05T07:40:34.881810+00:00 app[api]: Release v6 created by user ermiaskidanegebre@gmail.com 2018-07-05T07:49:13.302208+00:00 app[api]: Release v7 created by user ermiaskidanegebre@gmail.com 2018-07-05T07:49:13.302208+00:00 app[api]: Set DISABLE_COLLECTSTATIC config vars by user ermiaskidanegebre@gmail.com 2018-07-05T07:54:43.000000+00:00 app[api]: Build started by user ermiaskidanegebre@gmail.com 2018-07-05T07:55:09.406852+00:00 app[api]: Release v8 created by user ermiaskidanegebre@gmail.com 2018-07-05T07:55:09.406852+00:00 app[api]: Deploy 2fdb68de by user ermiaskidanegebre@gmail.com 2018-07-05T07:55:18.000000+00:00 app[api]: Build succeeded 2018-07-05T07:57:14.239360+00:00 app[api]: Starting process with command `python manage.py migrate` by user … -
curl invalid Syntax error
I was following the following tutorial click here on Step 4 when I run curl -X POST -d "grant_type=password&username=<user_name>&password=<password>" -u"<client_id>:<client_secret>" http://localhost:8000/o/token/ with my credentials, I get Syntax Error what am I doing wrong -
Refiltering through Model Querys in Django
I was wondering if you could save the output of a query in a views.py file, and then further query the saved output later. I need to use all of the information in all_games, and then I need a subset of all_games. It seemed silly to re-query the database since the data has already been fetched. Below code is the idea I was playing around with: all_games = Game.objects.filter(league=passed_dict['league'], season=passed_dict['current_season'], home_team=passed_dict['divisions'][i].teams[j]) # [Random Code] passed_dict['value'] = all_games.filter(season=passed_dict['current_season']) -
Foreign key reference with count
I am trying to get the count of foreign key(author) in order to get the count of total blogs made by a user. But it is showing the incorrect total. Can someone help on how to get the foreign key reference with count. Thanks. models.py class Blog(models.Model): author = models.ForeignKey(User, on_delete = models.CASCADE, related_name='blogs') created_date = models.DateTimeField(default=timezone.now) def total_likes_received(user): return user.blogs.aggregate(total_likes=Count('likes')) views.py def get_queryset(self): return (Blogger.objects.filter(date__lte=timezone.now()) .order_by('-date') .annotate( author_total_likes = Count('author__blogs__likes'), author_total_blogs = Count('author__blogs') ) -
Not PayPal integration
I have integrate PayPal payment gateway using JavaScript and payment is done successfully but it doesn't send any response parameters to indentify a success or failure message.? So give me suggestions to resolve this issue. -
Django ORM "get" override issue
I have a ContractGoal class with its own get / save methods and corresponding database functions to handle the data: models.py class ContractGoalManager(models.Manager): def get(self, contractid): return get_data_pk(self, 'SP_DCPGetContractGoal(%s)', (contractid,) ) def save(self, myContractGoal): return save_data('SP_DCPUpsertContractGoal', ( myContractGoal.contractid, myContractGoal.goalid, myContractGoal.difficultylevel, myContractGoal.goaldescription, myContractGoal.achievedflag, myContractGoal.acceptedflag, myContractGoal.rewardinfo, ) )[0] class ContractGoal(models.Model): contractid = models.IntegerField(primary_key=True) goalid = models.IntegerField() difficultylevel = models.CharField(max_length=1) goaldescription = models.CharField(max_length=500) acceptedflag = models.NullBooleanField() achievedflag = models.NullBooleanField() rewardinfo = JSONField() class Meta: managed = False objects = ContractGoalManager() def save(self): return ContractGoal.objects.save(self) SP_DCPGetContractGoal.spl CREATE OR REPLACE FUNCTION SP_DCPGetContractGoal(pContractId INTEGER) RETURNS SETOF Contract_Goal AS $$ BEGIN RETURN QUERY SELECT ContractId, GoalId, DifficultyLevel, GoalDescription, AcceptedFlag, AchievedFlag FROM Contract_Goal WHERE ContractId = pContractId; END; $$ LANGUAGE 'plpgsql'; test_contractgoal.py # Test case 1 - Retrieve existing goal from DB mygoal = ContractGoal.objects.get(contractid = 1, goalid=2) # Save goal (existing) mycontractgoal.difficultylevel = "D" mycontractgoal.save() # Test case 2 - Create new goal newgoal = ContractGoal( contractid = 1, goalid = None, difficultylevel = 'M', goaldescription 'Manual goal', acceptedflag = None, achievedflag = None, rewardinfo = json.dumps( <some_JSON_string> ) ) # Save goal (new) newgoal.save() When I run the code above, it gives me this error: Traceback (most recent call last): File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\utils.py", line 85, … -
Python XML validation against Schema lxml
I am trying to validate an XML document against its XSD in Python. I am currently following this tutorial. I am able to read in my XML file without issue. However I need to provide an encoding type for the schema. Failing to do so results in a 'charmap' codec can't decode byte 0x81 error. with open(os.path.join(settings.MEDIA_ROOT, xsd_filename), 'r', encoding='utf-8') as schema_file: schema_to_check = schema_file.read() However, when I add the encoding etree is unable to parse the contents of the file? The error returned is Unicode strings with encoding declaration are not supported. Please use bytes input or XML fragments without declaration. xmlschema_doc = etree.parse(StringIO(schema_to_check)) I'm not sure I know what's going on. From the lxml site I get the impression that I should leave the encoding alone but I'm not sure why the tutorial works and mine doesn't? My version of Python is 3.6.4. Thanks in advance. -
Django GenericForeignKey Referential INtegrity
I'm new to django programming, and trying to get to grips with GenericForeignKeys. I've managed to create a generic foreign key which stores in the database, but the model does not appear to enforce referential integrity, (i.e. I can create a reference to an ID of an object type where that object id does not exist) (eg: content_type: "post", content_id: 123, but post 123 does not exists). (Before you say it - Yes - I'm aware that Django has some facility for tagging - this is a really simple example/test for me to understand Generic Foreign Keys, and figure out how they work, so I can implement them in other situations) My basic model is: class Tag(NamedModel, models.Model): taggable_models = models.Q(models.Q(model='post') | models.Q(model='discussion')) content_type = models.ForeignKey( ContentType, models.CASCADE, limit_choices_to=taggable_models, ) content_id = models.PositiveIntegerField( _("content id"), ) content_object = GenericForeignKey( 'content_type', 'content_id', ) In my admin.py, I have: @admin.register(Tag) class TagAdmin(VersionAdmin): readonly_fields = ('created_at',) list_display = ('content_type', 'content_id', 'created_at', ) fieldsets = ( (None, { 'fields': ('content_type', 'content_id', 'created_at', ), }), ) autocomplete_lookup_fields = { 'generic': [['content_type', 'content_id']], } However, when I run the admin page, it will allow me to create a Tag against a post id that does not … -
running server at 2 diffrent ports in django
Can I run 2 servers(development) in Django and in each server I want 2 different users to log in simultaneously so that I can analyze the behavior of superuser and user simultaneously(as I am testing permissions) -
PyCharm + Python 3.6 + Django + debugging + generators == world of pain
I have this issue for a while now where starting django's internal server (runserver) becomes near unusable because there's so many errors like this reported in the console: Exception ignored in: <generator object SQLCompiler.setup_query.<locals>.<genexpr> at 0x2F2DE360> Traceback (most recent call last): File "C:\Python36\lib\site-packages\django\db\models\sql\compiler.py", line 39, in <genexpr> if all(self.query.alias_refcount[a] == 0 for a in self.query.alias_map): SystemError: error return without exception set Basically, these are generators not consumed and Python (at least 3.5 and up) reports this into console. And there are MANY! This basically hogs the python process serving the app as well as PyCharm process trying to display all these errors in the console view. On a bad day, the app becomes like 10% of its normal speed because of this. I am currently lessening this problem by implementing a filter on stderr which at least makes the console output usable again. It helps with CPU usage as well, but the problem is that those exceptions still happen and trigger PyCharm's hooks. As a result, CPU usage is still high, though not insane any more. How can I get rid of this permanently? Any interpretation of "get rid" accepted in proposed solutions. -
Django: Upload file via POST request
I'm trying to upload file via APIView, but I'm getting that exception: {"exception": "ValidationException", "code": 401, "message": "You cannot access body after reading from request's data stream"} Here's my code: API View: class SetAvatarView(LoginRequiredAPIView): @csrf_exempt # Does no affect to situation def post(self, request): try: request.account.update_avatar(request.FILES['file']) except ValidationException, e: return JsonResponse(e.to_dict(), status=400) return JsonResponse({}, 200) Account model: class Account(models.Model): ... avatar = models.ImageField(upload_to=AVATARS_URL, default='default.jpg') ... def update_avatar(self, f): self.avatar = f Test code for uploading file: def test_set_avatar(self): url = "/account/avatar/set/" with open("test.jpg", "r") as fp: response = self.client.post(url, {'file': fp}, #content_type='multipart/form-data', # Getting 400 Bad Request if uncomment **{'HTTP_AUTHORIZATION': 'Token 0ff0884090**********8ef5387423bc24cd15e1'}) print response.content self.assertEqual(response.status_code, 200) No additional middlewares in settings.py, and I tried to disable default middlewares, no result. -
DRF regex search
I need to search and return my products in a certain order and have a regex that handles the search, the problem is, I'm not sure how to apply it to the search field in DRF search_filters. Here's the regex that I would like to use "^(?P<name>\w+ ?,?)" - any name that starts with the entered text and possibly ends with a space or a comma. If possible I would also like to apply a tokenized ordering, so "rice", "rice " and "rice," are returned before "ricenoodle". I'm trying to use the regex like that search_fields = ['^(?P<name>\w+ ?,?)'] but all I get back is an error like that Cannot resolve keyword '(?P<name>\w+ ?,?)$' into field. DRF states that to use regex I need to use "$name", and has a link to django docs that have 0 info on how to construct search fields with a regex (https://docs.djangoproject.com/en/2.0/ref/contrib/admin/#django.contrib.admin.ModelAdmin.search_fields). How should I construct search_fields? -
Django view DB in the View
I have a model Class A name age username Class B () skin_color wight ForeignKey = 'A' Class C () gens blood_type ForeignKey = 'B' Class D () hours_of_working hours_of_sleep ForeignKey = 'B' i want to Display Class A object and It's related relation via views.py each to a specific name. -
Django error message in class based view
Can I change the error message from the model for a class-based view? the following is not working, it always gives the default message from Django. Model: class Child(models.Model): name = models.CharField(max_length=100, error_messages={ 'blank': 'cannot be blank or null', 'null': 'cannot be blank or null',}) view: class ChildCreate(CreateView): model = Child fields = '__all__' success_url = reverse_lazy('children-list') -
Django - post InMemoryUploadedFile to external REST api
In the Django Rest Framework I would like to post a file, received as an InMemoryUploadedFile, to a different server as soon as it is received. It sounds simple, but the request.post() function does not seem to properly send over such a file : def post(self, request, *args, **kwargs): data = request.data print(data) # <QueryDict: {'file': [<InMemoryUploadedFile: myfile.pdf (application/pdf)>]}> endpoint = OTHER_API_URL + "/endpoint" r = requests.post(endpoint, files=data) My other server receives the request (through flask) with the name of the file, but not the content: # After saving the file to path, I get: print(path) # /src/dir/myfile.pdf => server found the file name print(os.path.getsize(path)) # 0 Any help on how to fix this, i.e. transform the InMemoryUploadedFile type in something a normal REST api can understand ? -
Connection broken: IncompleteRead(0 bytes read) when sending request to Django on Heroku
I have a view that returns a JsonResponse in Django def some_view(request): return JsonResponse({'a': 1, 'b': 2}) And somewhere else in side the same project, I have a piece of code that sends a GET request to the url corresponds to that view: import requests def client_func(): requests.get(url_to_some_views) It works on my local machine but after deploying to heroku, sometimes I get the error: ('Connection broken: IncompleteRead(0 bytes read)', IncompleteRead(0 bytes read)) After some testing, I found out that there is an extra field in the response header of some_view: transfer-encoding: chunked so I guess it might be what caused the problem but I'm still not sure how to handle this. So far I have tried to add stream=True to the get call in client_func but the problem still persists. Can someone help me with that might be the cause of my problem and to solve this please. Thank you very much! -
Flask : How to start the server
I am using this Package from github, I am having difficulty in starting the server, The question seems not appropriate but they have nothing related to server starting. Any python flask user can explain the situation here. Note: Searched alot here and there but nothing got from google or from the package documentation. Had to ask the question. -
Using Bootstrap-4 in Django 1.11
{% block content %} <h1>Hello Customer</h1> <div class="control-group"> {{r_form.as_p}} {{c_form.as_p}} <button class='btn btn-success' type="submit">Submit</button> </div> {% endblock %} How to apply bootstrap to the above fields? Have tried many ways still am not able to apply. -
No module named PIL in heroku though it is installed
I have been trying to deploy a Django application on Heroku. Some background - I hadn't used virtual environment while building the application but while deploying it, I had to use it. I'm following this tutorial for deployment. https://medium.com/agatha-codes/9-straightforward-steps-for-deploying-your-django-app-with-heroku-82b952652fb4 Now I'm stuck in step-6. After ceating a virtual environment I have freezed the dependencies into requirements.txt. But when I open the deployed URL it says ImportError at / No module named PIL though Pillow has been installed, it is there in requirements.txt and runs fine when hosted on a local server. This is my requirements.txt. certifi==2018.4.16 dj-database-url==0.5.0 Django==1.11.14 django-heroku==0.3.1 gunicorn==19.9.0 packaging==17.1 Pillow==5.2.0 pip-review==1.0 pipenv==2018.7.1 pkg-resources==0.0.0 psycopg2==2.7.5 pyparsing==2.2.0 pytz==2018.5 six==1.11.0 virtualenv==16.0.0 virtualenv-clone==0.3.0 whitenoise==3.3.1 Please help me with this. -
How can I make this queryset more tolerable?
I have the following class: class Instance(models.Model): products = models.ManyToManyField(Product, blank=True) class Product(models.Model): description = HTMLField(blank=True, null=True) short_description = HTMLField(blank=True, null=True) And this form that I use to update Instances class InstanceModelForm(InstanceValidatorMixin, UpdateInstanceLastUpdatedMixin, forms.ModelForm): class Meta: model = Instance products = forms.ModelMultipleChoiceField(required=False, queryset=Product.objects.annotate(i_count=Count('instance')).order_by('i_count')) My instance-product table is sizable (~ 1000 rows) and ever since I've added the queryset for products I am seeing web requests that are timing out due heroku's 30 second request limit. My goal is to do something to this queryset such that my users are no longer timing out. I have the following insights: Accuracy doesn't matter as much to me - It doesn't have to be very accurate. Yes I would like to sort products by the count of instances this product has linked to but if it's off by 5 or 10 it doesn't really matter that much. Limited number of products - When my users are selecting products to be linked to an instance, they are primarily interested in products with less than 10 total linkages to instances. I don't know if a partial query will be accurate, but if this is possible I am open to trying. Effort - I know there … -
How to run a Django server over a system which has a proxy setting over lan without removing the proxy setting
I have a Django project which run fine but the laptop uses a proxy setting over LAN and hence the server does not run unless I don't remove proxy setting . I wish to amend changes in my Django project to make it compatible to the proxy settings. -
Django admin query to list specific records
Say I have a model named Foobar in the foobar app, to list the record #1 in the back office, the url will be : http://djangosite.com/admin/foobar/foobar/?q=1 Is it possible to have a valid url to list multiple records, for example records #1 AND #6 AND #42 ? -
Handling Django user subscription expiry date
I have a User model which extends AbstractBaseUser class User(AbstractBaseUser): email = models.EmailField(max_length=255, unique=True, default='abc123@gmail.com') forename = models.CharField(max_length=20, default='') surname = models.CharField(max_length=20, default='') account_expiry = models.DateField(default=datetime.now() + timedelta(days=365)) In all of my views I authenticate using the LoginRequiredMixin e.g. class IndexView(LoginRequiredMixin, generic.TemplateView): I want to authenticate users not just by email and password, but also by checking that the account expiry date has not been reached. My question is - what is the best (simplest?) way to achieve this. Do I write my own custom authentication backend as described here? Or should I be looking to write a custom Mixin or Middleware?