Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Reconstructing a formset
Django 1.10. Studying formsets. Interested in method has_changed. Trying to learn by analogy with forms: "When the form is submitted, we reconstruct it and provide the original data so that the comparison can be done" (https://docs.djangoproject.com/es/1.10/ref/forms/api/#checking-which-form-data-has-changed). So, at the server we need to save initial data. Then Django renders html: a form is solitary, our formset being just input tags with special ids. User submits the form. We catch request.POST. Here we have to recunstruct the formset. The problem is that being submitted, the formset looks like this: QueryDict: <QueryDict: {'csrfmiddlewaretoken': ['X5RMIXfwfQH9zUYz7ODw59UPEr1AQaeciJR2qrLU64ipbBnbu4jKEcW0nVc019vC', 'X5RMIXfwfQH9zUYz7ODw59UPEr1AQaeciJR2qrLU64ipbBnbu4jKEcW0nVc019vC'], 'form-0-pub_date': ['2016-01-01'], 'form-1-pub_date': [''], 'form-0-title': ['Initial title'], 'form-1-title': ['']}> To the best of my ability, I only can think of: 1. Getting keys: request.POST.keys() Result: dict_keys: dict_keys(['csrfmiddlewaretoken', 'form-0-pub_date', 'form-1-pub_date', 'form-0-title', 'form-1-title']) 2. splitting the keys like str.split('-'). Then I'll have somehow handle the data. def post(self, request): l = list(request.POST.keys()) l.remove("csrfmiddlewaretoken") for element in l: sp = element.split("-") pass Well, this is all cumbersome. And I feel clumsy. So clumsy that I don't even know whether there is a practical value in the has_changed method. Could you suggest me an elegant way to reconstruct a formset. -
Is it possible to test a REST API against documentation in Django?
I develop a RESTful API server using Django REST Framework, and while the app matures, entity signatures sometimes change. While writing tests for this app, I began to wonder if there are tools to check that API returns data as stated in documentation, i.e. User entity contains all the required fields etc. I know that there are API auto-documenting tools like django-rest-swagger and others, and maybe there is some tool that helps asserting that data returned to user has same signature as in documentation? -
Django filter with ForeignKey not working
I am building ecommerce and having trouble creating dashboard for sellers. No matter how I try to get filter processed orders so I can show sold products to sellers I couldn't make it happen. Some help will be a great relief. Following is my Seller Mixin I am trying to create: mixins.py class SellerAccountMixin(LoginRequiredMixin, object): account = None products = [] transactions = [] orders = [] def get_account(self): user = self.request.user accounts = SellerAccount.objects.filter(user=user) if accounts.exists() and accounts.count() == 1: self.account = accounts.first() return accounts.first() return None def get_products(self): account = self.get_account() products = Product.objects.filter(seller=account) self.products = products return products def get_all_products(self): account = self.get_account() products = Product.objects.all() self.products = products return products def get_sold_products(self): orders = UserCheckout.objects.get(user_user=self.user) return orders seller/views.py: class SellerDashboard(SellerAccountMixin, FormMixin, View): form_class = NewSellerForm success_url = "/seller/" def post(self, request, *args, **kwargs): user = self.user form = self.get_form() if form.is_valid(): return self.form_valid(form) else: return self.form_invalid(form) def get(self, request, *args, **kwargs): apply_form = self.get_form() #NewSellerForm() account = self.get_account() exists = account active = None context = {} if exists: active = account.active if not exists and not active: context["title"] = "Apply for Account" context["apply_form"] = apply_form elif exists and not active: context["title"] = "Account Pending" elif … -
Django wiki (from its official website)
Choosing a wiki engine. Django has a wiki at its official site. https://code.djangoproject.com/wiki I suppose this wiki engine must me rather stable and well supported. Maybe this is the best choice. Could you tell me what is the name of this wiki application. -
Django 1.10 - how to load initial users
What is the right way to load initial users in Django 1.10? When we talk about our own django app then it is recommended to have a /fixtures/initial_data.json (as mentioned here). But in the case of User, where django.contrib.auth is not our app, where should we put the fixtures directory and how do we load it? Thank you, Rami -
pip install Django hangs
Recently I have been unable to install Django 1.10 (or earlier version) using pip install django inside or outside a virtual enviornment. After running the command pip hangs on the line 'Collecting Django' and does nothing. I ran the command in verbose (-vvvvv) mode and get this: Collecting django Getting page https://pypi.python.org/simple/django/ Starting new HTTPS connection (1): pypi.python.org "GET /simple/django/ HTTP/1.1" 200 12273 Then it just hangs without an error message. The only way I can install Django is running pip with sudo, but I want to install it inside a virtual environment, not globally. I can install other packages without any problem. I am on OS X El Capitan. -
Django: Form submission clears the data of inlineform in template rendering if error in form validation
I hvae a django inlineformset. I am using a form and inlineformset in template. However when user click on submit and if there is any error in inv_form, the form gets rendered back with validation errors. However Inlineformset becomes empty but form has user data. What should I change in my code so that if anything is wrong in form, inlineformset values are not cleared. View code (only validation code): if request.method == 'POST': inv_form = createinvoiceform(request=request,data=request.POST,user=request.user) if inv_form.is_valid(): new_form=inv_form.save(commit=False) new_form.user=request.user new_form.company_det=request.user.company.entity new_formset=createinvoiceformset(request.POST,instance=new_form) gv_formset=invoicegbformset(request.POST,instance=new_form) if new_formset.is_valid() and gv_formset.is_valid(): new_form.save() hero=new_formset.save(commit=False) for hero1 in hero: hero1.user=request.user hero1.company=new_form.company_det hero1.UOM=hero1.Product hero1.save() formtosave=gv_formset.save(commit=False) for data in formtosave: data.user=new_form.user data.generalcomp=new_form.company_det data.generalamount = -data.amount data.Credit=data.amount data.date=new_form.invoice_date data.type="Tax" data.save() -
Retrieve the last migration for a custom migration, to load initial data
I created a custom migration "0000_initial_data.py" and I want that to get applied after all the other migrations are done. Though when I try to use ____latest____ in the dependencies I get "dependencies reference nonexistent parent node" error I feel it is trying to find ____latest____ named migration in the folder but it is unable to find. I got an idea of finding the latest migration in myapp/migrations/ using "ls -Art | tail -n 1" which gave me 0001_initial.pyc [pyc] file rather than the latest migration .py file. Though even if I get the name of the latest migration I have to replace in the custom migration file using a shell script like $ replace '__latest__' 'output of ls -Art' -- 0000_initial_data.py as am automating my deployment. I would like to know the best way to get the latest migration from all myapps in the project and plug my custom migration after it. Note: using django==1.8.13, ubuntu 14.04, python 2.7 -
run celery tasks in random times
i need to run few celery tasks in random times - every run should be at a new random time - the random number should generated every run. what I did in the past is: "my_task": { "task": "path.to.my_task", "schedule": crontab(minute='*/%s' % rand), }, rand = random(1,12) but this code is not good for my needs anu more: 1. I need different (as possible with random0 number for each tenant 2. different number will generated every time and not only on settings.py load (once) I tried to overwrite Schedule as explained in THIS answer but it did not work, is there better way? am I missing something? (for example in tenant A the task will run at 23 and the day after at 8, and in tenant B the task will run at 4 and the day after at 20 etc) Thanks! -
django-nginx 502 after adding python-cors-middleware
I tried adding python-cors-middleware on django1.10 to allow CORS requests, all tests passed locally but now NGINX is throwing a 502 with the following error in logs... 2016/09/25 02:17:14 [error] 10846#0: *31 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 220.90.173.169, server: example.com, request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:9000/favicon.ico", host: "example.com", referrer: "https://example.com/" 2016/09/24 18:26:54 [error] 29546#0: *85254 access forbidden by rule, client: 66.249.84.168, server: example.com, request: "GET /en/ HTTP/1.1", host: "example.com" There are multiple instances of both types of errors. Do I have to change something in nginx or should I be looking to change something in Django? -
How do I use socket objects inside Celery tasks using Django?
I am stuck with an issue regarding serializing socket objects for a long time and need help. Before I state my problem, here is the technology stack of the application I am building. Django-1.8.7 Redis Celery MySQL gevent-socketio greenlet Here is the background of the problem : I want to send notifications asynchronously in my application without making the request wait for the notifications to be sent to the subscribers. I am using Celery to run the tasks for sending notifications via sockets which are created when a user logs in into the application. The problem is Celery doesnt recognize the sockets created inside the django application. My attempts : I tried multiple ways to serialize the sockets object and pass it to Celery task but I keep getting this error. "Socket Object cannot be serialized" I also tried storing the sockets in cache so that I can retrieve from the cache during the Celery task. But then I get this error while inserting into cache - "cannot serialize 'Hub' object" or ""cannot serialize 'Greenlet' object" Also took help of the below answer, but no luck. http://stackoverflow.com/a/26164781/6876786 Can someone help me fix this problem? Thanks in Advance! -
Django Form: form with ForeignKey
I'm making online shopping mall using Django(1.9.7) framework. I think that showing codes is much easier than explaining in text. models.py class Product(TimeStampedModel): name = models.CharField(max_length=120, unique=True) slug = models.SlugField(null=True, blank=True) description = models.TextField(max_length=400, blank=True) is_active = models.BooleanField(default=True) def __str__(self): return self.name class Variation(TimeStampedModel): COLOR_CHOICES = ( ('black', '흑백'), ('single', '단색'), ('multi', '컬러'), ) price = models.DecimalField( decimal_places=0, max_digits=15, blank=True, null=True, ) product = models.ForeignKey(Product) color = models.CharField( max_length=10, choices=COLOR_CHOICES, ) is_active = models.BooleanField(default=True) class Meta: unique_together = (('product', 'color')) def __str__(self): return str(self.product) + ' - ' + self.get_color_display() I create form in my product_detail view and pass it as context data to template. views.py class ProductDetailView(DetailView): model = Product context_object_name = "product" def get_context_data(self, *args, **kwargs): context = super().get_context_data(*args, **kwargs) product = self.get_object() context['cartitem_form'] = CartItemForm(product) return context What I want to do through form: I want to show variations only related with given product. So I pass product as argument of form in view and save this product. And I'm trying to set the variation queryset through ModelChoiceField: class CartItemForm(forms.ModelForm): variation = forms.ModelChoiceField( queryset=Variation.objects.filter(product=self.product) ) class Meta: model = CartItem fields = ( 'variation', 'width', 'height', 'quantity', ) def __init__(self, *args, **kwargs): self.product = kwargs.pop('product') super().__init__(*args, **kwargs) … -
Why do direct imports for view fail in urls.py?
If I have two python modules in a directory main.py and somemodule.py, I can import somemodule by using import somemodule. In django application where we have urls.py and views.py, why won't import views work in this case? But relative import from . import views works? -
Django: What do I pass in as self in the view.py if I am using a function from model.py?
I created a simple Django view that is suppose to print out the list of names field from my model. I created a function inside my User model to query the names, and append it to a list, and return that. I want to call this function inside my view class, and pass that list as a context so I can use it inside my template. Below is my current code. I am not sure what self is suppose to be when I am trying to call it in views.py. When I try passing in my model name, which is User, it did not work. So what exactly is self suppose to be in the views.py? Thanks in advance:D User models.py def query_choice(self,query_choice): users_first_name = self.objects.values_list(query_choice) names = [] for u in users_first_name: names.append(u) return names views.py def login(request): users_first_name = User.query_name(*self*,'first_name') template = loader.get_template('registration/home.html') context = { 'output': users_first_name, } #return HttpResponse(output) return HttpResponse(template.render(context,request)) -
how can I modify the site packages on heroku because of this error 'PeriodicTask' object has no attribute '_default_manager'
I am developing my site locally and I was getting an error like this File "/app/.heroku/python/lib/python3.5/site-packages/djcelery/schedulers.py", line 98, in save obj = self.model._default_manager.get(pk=self.model.pk) AttributeError: 'PeriodicTask' object has no attribute '_default_manager' so I changed it to this Model = type(self.model) obj = Model._default_manager.get(pk=self.model.pk) I thought that when i did a git heroku push it would change accordingly. But it did not, it only affects the local environment. which makes sense, because heroku uses requirements.txt for the dependencies it downloads into itself. But how can I fix this because it won't work in deployment. I thought I figured it out but that was just locally where it was rectified remotely on heroku is another issue. -
Django ORM: Models with 2 table referencing each other
I have 2 tables. User and Group. 1:Many relationship. Each user can only belong to a single group. here's the model.py. class Group(models.Model): group_name = models.CharField(max_length=150, blank=True, null=True) group_description = models.TextField(blank=True, null=True) group_creator = models.ForeignKey(User, models.DO_NOTHING) class User(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30) ... group = models.ForeignKey(Group, models.DO_NOTHING) The issue I have is that they are both referencing each other which is acceptable in MySQL and Oracle, but, I get an error when migrating: group_creator = models.ForeignKey(User, models.DO_NOTHING) NameError: name 'User' is not defined Now when I reverse the order (so, User first than Group), I get group = models.ForeignKey(Group, models.DO_NOTHING, blank=True, null=True) NameError: name 'Group' is not defined This is getting quite frustrating. I have a few work around (make it a many:many and keep creator on Group class), but before I start destroying my datamodel and move data move all the data around, I wonder if anyone has this issue before. How did you solve this? Do you really have to change your datamodel? -
Extend Django user class or create new class
I am a programming noob and am building a simple Django app with a user profile that will include things like favorite books, about etc. From an archetectual stand point, would it be better to extend the User class with these fields or create a new UserProfile class that adds these as a one to one with the User class? -
Different django settings moodule in different virtualenvs
I want to have specific DJANGO_SETTINGS_MODULE in a specific virtualenv. E.g: in tb_test the testing should be testing.py in tb_dev the settings should be development.py Here is my project structure: In files development.py and testing.py i have the same code: # -*- coding: utf-8 -*- from .base import * I've tried to use export DJANGO_SETTINGS_MODULE="task.settings.development" under [tb_dev] env in terminal and export DJANGO_SETTINGS_MODULE="task.settings.testing" under [tb_dev] env in terminal But then in [tb_test] it's still using "task.settings.development" Also I've tried to add export DJANGO_SETTINGS_MODULE="task.settings.testing" to $VIRTUAL_ENV/bin/active under [tb_test] export DJANGO_SETTINGS_MODULE="task.settings.development" to $VIRTUAL_ENV/bin/active under [tb_dev] But this also doesn't work. Im using Django 1.9.0 also virtualenvwrapper and virtualenv. Could you help me to solve this problem? -
How to reduce time to first byte (TTFB) of Django website
I'm trying to reduce a webpage time to first byte (TTFB). The current TTFB is 1300ms. My goal is to reduce this to under 600ms. The view requires 2 database accesses and a small loop with roughly 80 iterations. I've optimized my code significantly. I don't think anything else can be done to the code. My current set-up: Server: EC2 t2.small (Linux) Database: RDS db.t2.small I'm thinking of upgrading my database to db.t2.medium. Any ideas on what upgrades I should make. Here is the waterfall from webpagestest.org -
Cant print out request.body in Django
When I try to print out request.body print(str(getattr(request, 'body')) I get the following error? int(self.META.get('CONTENT_LENGTH', 0)) > settings.DATA_UPLOAD_MAX_MEMORY_SIZE): -
I have afunction that works locally on my django app but deployed it raises an list index out of range error
I have a function that raises an error in my logs when I try to use it remotely. I have it run in my background task. the other task I have runs both locally and remotely. heres my code my my_scraps.py def liveleak_task(): url = 'http://www.example1.com/rss?featured=1' name = 'live leak' live_leaks = [i for i in feedparser.parse(url).entries] the_count = len(live_leaks) live_entries = [{'href': live_leak.links[0]['href'], 'src': live_leak.media_thumbnail[0]['url'], 'text': live_leak.title, 'comments': live_leak.description, 'name': name, 'url': live_leak.links[0]['href'], # this is the link to the source 'embed': live_leak.links[1]['href'], 'author': None, 'video': False } for live_leak in live_leaks][10] return live_entries def worldstar_task(): # scraping from worldstar url_to = 'http://www.example2.com' html = requests.get(url_to, headers=headers) soup = BeautifulSoup(html.text, 'html5lib') titles = soup.find_all('section', 'box') def make_soup(url): the_comments_page = requests.get(url, headers=headers) soupdata = BeautifulSoup(the_comments_page.text, 'html5lib') comment = soupdata.find('div') para = comment.find_all('p') kids = [child.text for child in para] blu = str(kids).strip('[]') return blu name = 'World Star' cleaned_titles = [title for title in titles if title.a.get('href') != 'vsubmit.php'] world_entries = [{'href': url_to + box.a.get('href'), 'src': box.img.get('src'), 'text': box.strong.a.text, 'comments': make_soup(url_to + box.a.get('href')), 'name': name, 'url': url_to + box.a.get('href'), 'embed': None, 'author': None, 'video': True } for box in cleaned_titles][:10] return world_entries def scrambled(a, b): site_one = a site_two = … -
Django throwing ValueError when trying to print values of dir(request)
For debugging purposes I have the following code def some_view(request): target = request t = ((x, str(getattr(target, x))) for x in dir(target)) return '\n'.join(map(str, t)) It works when I use it on request.session object But raises this weird error when I try it on request itself Anyone know how to solve this? I just want a print out of all an objects attributes and their respective str(x) for debugging... -
Exception when migrating custom User in Django
I need to create a custom User for my app and followed exactly the example from the documentation with the AUTH_USER_MODEL = 'core.MyUser' in my settings.py. However, when I make a new database, delete all the migrations folders and run the python manage.py migrate again, it gives me the exception like this Applying auth.0009_customer...Traceback (most recent call last): .... .... TypeError: Customer cannot proxy the swapped model 'core.MyUser'. I am not sure why there is a migrations script for the customer there, since in my app, I used to have the Customer model as well, though I deleted it already. Then, I created a new django project to test and try to run the migration. Surprisingly, I also see those customer migration steps, but it run successfully. Applying auth.0008_alter_user_username_max_length... OK Applying auth.0009_customer... OK Applying auth.0010_delete_customer... OK Applying sessions.0001_initial... OK In short, how can I create the custom User in Django 1.10 ? -
How to set Django to Ignore mysql errors and force mysql to insert a record?
So I am a naive Django developer and want a similar functionality as INSERT IGNORE of mysql in Django. Is there a way to do that? Right now whenever I try to save a record in my database, It throws (1364, Field doesn't have a default value). Should there be any change in Django settings or mysql settings? -
Edit multiple records in one template
I'm not at all sure that my approach to this problem is valid, so I'll appreciate "strategic" recommendations. From the other hand I hope to get some educational value from resolving the problem as it is. I have a model Record and I want to edit values of several instances of it at once, on the same page. That is ~how my edit form should looks like For that I created this template: **template.html** #with some omissions {% for record in records %} <td> <input type ="text" name="position_for_record_{{ record.id}}" value ="{{ record.position }}" size ="3"> </td> <td> <input type ="text" name="hours_for_record_{{ record.id }}" value = "{{ record.hours }}" size ="3"> </td> <td> <input type ="text" name="tips_for_record_{{ record.id}}" value = "{{ record.tips }}" size ="3"> </td> {%endfor%} After submitting this form I'll get all those fields in the request.POST, the question is how to extract them? I can, of course, iterate through ALL records **views.py** def form_returns_here(request): all_records=Record.objects.all() for record in all_records: if "position_for_record_"+str(record.id) in request.POST: But it just doesn't seem right. Thank you.