Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
I have created many apps in my project. how can I group them now?
If I have created many apps in the root directory, how to group them ? In SO, I find a method to group apps: How do I create sub-applications in Django? They all are create the folder first, then startapp like: python manage.py startapp sub_app1 app/sub_app1 The app/ is the folder to group the apps. But my case is I have created many apps in my project. how can I group them now? -
UpdateView doesnt save the avatar field?
I have abstract User with avatar field. from django.db import models from django.contrib.auth.models import AbstractUser class User(AbstractUser): bio = models.TextField(max_length=500, blank=True) location = models.CharField(max_length=30, blank=True) birth_date = models.DateField(null=True, blank=True) avatar = models.ImageField(upload_to='avatars/', null=True, blank=True) And views.py @method_decorator(login_required, name='dispatch') class UserUpdateView(UpdateView): model = User fields = ('first_name', 'last_name', 'email', 'bio', 'location', 'birth_date', 'avatar', ) template_name = 'my_account.html' success_url = reverse_lazy('my_account') def get_object(self): return self.request.user I can see fields and can save all of it except avatar . What to do? -
Connection timeout error in sending an smtp mail through zoho
Am getting a connection time out error when am trying to send a django mail through smtp. Below is my configuration - EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' # Host for sending e-mail. EMAIL_HOST = 'smtp.zoho.com' # Port for sending e-mail. EMAIL_PORT = 587 # Optional SMTP authentication information for EMAIL_HOST. EMAIL_HOST_USER = 'cs@amaze.com' EMAIL_HOST_PASSWORD = 'amaze123' EMAIL_USE_TLS = True And the code which am using is : from django.core.mail import send_mail >>> send_mail('Order Confirmation', 'Order placed successfuly', 'cs@amaze. com', ['check@gmail.com']) Error - Traceback (most recent call last): File "<console>", line 1, in <module> File "/usr/local/lib/python2.7/dist-packages/django/core/mail/__init__.py", li ne 61, in send_mail return mail.send() File "/usr/local/lib/python2.7/dist-packages/django/core/mail/message.py", lin e 292, in send return self.get_connection(fail_silently).send_messages([self]) File "/usr/local/lib/python2.7/dist-packages/django/core/mail/backends/smtp.py ", line 100, in send_messages new_conn_created = self.open() File "/usr/local/lib/python2.7/dist-packages/django/core/mail/backends/smtp.py ", line 58, in open self.connection = connection_class(self.host, self.port, **connection_params ) File "/usr/lib/python2.7/smtplib.py", line 256, in __init__ (code, msg) = self.connect(host, port) File "/usr/lib/python2.7/smtplib.py", line 316, in connect self.sock = self._get_socket(host, port, self.timeout) File "/usr/lib/python2.7/smtplib.py", line 291, in _get_socket return socket.create_connection((host, port), timeout) File "/usr/lib/python2.7/socket.py", line 571, in create_connection raise err error: [Errno 110] Connection timed out -
Scheduling a task with Django, celery and Redis
I added a task in Celery for data exportation. I want that when a user calls on the Export all button, the task of exportation starts and there is a django message shown on the frontend while the exportation continue in the back ground. But from what I'm able to do, I have this in views.py : def CasesPaluExport(request): export_cases_palu.delay(request=request) messages.success(request, 'CasesPaluExport') return JsonResponse({"Ok": "ok"}, safe=False) and this in tasks.py: def export_cases_palu(request): try: plaintext = get_template('stock/download_case_palu.txt') htmly = get_template('stock/download_case_palu.html') d = Context({'username': request.user.username}) subject, from_email, to = 'Download all cases palu', settings.DEFAULT_FROM_EMAIL, request.user.email text_content = plaintext.render(d) html_content = htmly.render(d) msg = EmailMultiAlternatives(subject, text_content, from_email, [to]) msg.attach_alternative(html_content, "text/html") msg.send() except: logging.warning("Tried to send download email to user {0}".format(request.user)) The problem is that whenever I call that CasesPaluExport function, I get an error at the export_cases_palu.delay(request=request) line: Internal Server Error: /user/casepaluexport/ Traceback (most recent call last): File "/home/user/.virtualenvs/project/local/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner response = get_response(request) File "/home/user/.virtualenvs/project/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response response = self.process_exception_by_middleware(e, request) File "/home/user/.virtualenvs/project/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/home/user/.virtualenvs/project/local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view return view_func(*args, **kwargs) File "/home/user/project/user/views.py", line 170, in CasesPaluExport export_cases_palu.delay(request=request) File "/home/user/.virtualenvs/project/local/lib/python2.7/site-packages/celery/app/task.py", line 413, in delay return self.apply_async(args, kwargs) File … -
Heroku threaded file upload block
I'm using gunicorn with gevent workers to serve my Django app, locally I'm running it with docker In my local docker setup everything works, the file is transferred after a response to browser has been sent, this is a (simplified) file transfer func that I'm using: def transfer(local_path, s3_path): with local_storage.open(local_path, 'rb') as f: remote_storage.save(s3_path, f) And I'm starting this upload in separate thread: Thread(target=transfer, args=('/file.txt', 'file.txt')).start() (I'm not waiting for thread's join()) Recentrly I deployed my playground app to heroku (same setup gunicorn+gevent) and when hosting on heroku response is not returned to the browser until all threaded uploads are done, effectively beating the point of using this technique. Does heroku python3.6 runtime changes anything with python threading? I know that this isn't the correct solution, and in prod I'll be using direct to s3 uploads, but I'm still curious why I'm seeing that kind of behaviour on heroku? -
Django REST adding fields to ModelSerializser
I read Django REST Framework: adding additional field to ModelSerializer But it is not fit to my needs because it is read-only field Gists: https://gist.github.com/elcolie/c319a1df7cd11c4af68c54ab782bbedd https://gist.github.com/elcolie/256ce3b5cc0ee5598d2b7427f5ad8d18 Problem: https://gist.github.com/elcolie/fa084e6a243a5c36c20042324fb212c2 Traceback: https://gist.github.com/elcolie/c53d7efb4423f07e8de4907da5848493 Goal: Single serializer validate the input of create_order method And it be able to return the response Am I missing something? -
Django get_or_create() dublicates in db
Using Django 1.11.6, MySql I`m importing uniq only data rows from CSV file (~530 rows). After 1st import - all 530 records updated to the DB. If I import this file 2d time and ~30 last records will be updated to DB. Get data: obj.account = int(ready_item[0].replace("\"","").replace("*","")) pai_obj.reporting_mask = str(ready_item[0].replace("\"","").replace("*","")) pai_obj.group = ready_item[1].replace("\"","") pai_obj.location = ready_item[2].replace("\"","") pai_obj.terminal = ready_item[4].replace("\"","") pai_obj.settlement_type = ready_item[5].replace("\"","") pai_obj.settlement_date = datetime_or_none(report_data) pai_obj.amount = float_or_none(ready_item[6].replace("\"","").replace("$","").replace(",","")) data.append(pai_obj) Import vie get_or_create(): for record in data: Accountmode.objects.get_or_create( account=record.account, reporting_mask=record.reporting_mask, group=record.group, location=record.location, terminal=record.terminal, settlement_type=record.settlement_type, amount=record.amount, defaults={'settlement_date': record.settlement_date}) The Model: class Accountmode(models.Model): account = models.IntegerField(blank=True, default=0) reporting_mask = models.IntegerField(blank=False, default=0) group = models.CharField(max_length=1024, blank=True, null=True) location = models.CharField(max_length=1024, blank=True, null=True) settlement_date = models.DateField(null=True) terminal = models.CharField(max_length=1024, blank=False, null=True) settlement_type = models.CharField(max_length=1024, blank=False, null=True) amount = models.DecimalField(max_digits=25, decimal_places=2) created_date = models.DateTimeField(default=datetime.now, blank=True) As I know, get_or_create() should check if data already exist first and create new record if Not. Any ideas Why get_or_create() pass some records ? -
Django - Update User at login
I apologize if a question of the same order have been posted but I have found nothing. I'm actually working on a django project and i'm migrating to a CustomUser model. On the database everything have gone well and now i want to force my user to update their informations (to respect the new model) I would like to do it when they login (I set all the e-mail to end with @mysite.tmp in order to know if they have already update it or not). So if the e-mail end with this they should be automatically redirected to the update user view (without having been logged) and when they submit the form they get logged in (if the form is valid of course). So my question is how to make this happened at login? I could override the login function but it does not seems to be the better option. Is there a view that i can override? What will you recommend to me? PS: I'm working with django 1.11 and python 3.6 -
pytz.exceptions.AmbiguousTimeError: 1978-10-01 00:00:00, is there a proper way to hadle this daylight saving time?
i'm having this exception in my Django project, is there a proper way to handle this case? it's all about this specific daylight saving time. -
Django: Logging configuration location
I have on top of my views.py the following line to enable logging: logger = logging.getLogger(__name__) To change the loglevel to debug for all loggers I have put the following lines into the settings.py: if DEBUG: logging.getLogger().setLevel(logging.DEBUG) But still if I log something with loglevel INFO in my views.py it's not getting shown. Why? -
Send a request in url at a specific time in django
I want to ask for url at a certain time regardless of date. first i try like this url(r'^(?P<date>\d{4}-\d{2}-\d{2})/$', views.timer, name='timer'), I have already created the logic in views.py and can only request it. Is it a situation where I have to study more regular expressions? I have not been able to solve it for three hours now. It is so desperate. Please give me a little hint -
DJANGO importing fields to model from another model
I have the following Models and I'm try to get the vector in Especie.zonas to be a field in the model Zona. i.e. Especie.zonas is a vector of Zonas (model Zona) and I want it to have a OneToOne relationship with the model EspecieZona Models.py class Zona(models.Model): codigo = models.CharField(max_length=120) area = models.CharField(max_length=120) especies = models.ManyToManyField("Especie", blank=True) def __str__(self): return self.codigo def get_especies(self): return self.especies.all().values_list('nome', flat=True) class Especie(models.Model): nome = models.CharField(max_length=120) nome_latino = models.CharField(max_length=120) data_insercao = models.DateTimeField(auto_now_add=True) actualizacao = models.DateTimeField(auto_now=True) zonas = models.ManyToManyField("Zona",blank=True ) def get_zonas(self): return self.zonas.all().values_list('codigo', flat=True) def __str__(self): return self.nome class EspecieZona(models.Model): idEspecie = models.OneToOneField("Especie") here_is_my_problem = models.Especie.zonas() idZona = models.OneToOneField("Especie.zonas") fechado = models.BooleanField() def __str__(self): return str(self.idEspecie)+' em '+str(self.idZona) Thanks in advance! -
must be of the form 'app_label.ModelName'." % model ValueError: Invalid model reference
When I python3 manage.py makemigrations, I get bellow error: ... File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/models/fields/related.py", line 348, in contribute_to_class lazy_related_operation(resolve_related_class, cls, self.remote_field.model, field=self) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/models/fields/related.py", line 85, in lazy_related_operation return apps.lazy_model_operation(partial(function, **kwargs), *model_keys) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/models/fields/related.py", line 83, in <genexpr> model_keys = (make_model_tuple(m) for m in models) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/models/utils.py", line 23, in make_model_tuple "must be of the form 'app_label.ModelName'." % model ValueError: Invalid model reference '管理员后台.产品配置.qiyun_admin_productconfig_cloudserver.HostType But, my HostType model path is this : 管理员后台.产品配置.qiyun_admin_productconfig_cloudserver.models.HostType. The traceback less the .models in it. I don't know why. My project directory is bellow: Please PAY ATTENTION, the serializer and views(serializer view) is under the api directory. and the settings: INSTALLED_APPS = [ 'django.contrib.admin', .... '旗云管理员后台.用户管理.qiyun_admin_useradminmanage', # '旗云管理员后台.用户管理.qiyun_admin_usergroups', # '旗云管理员后台.产品配置.qiyun_admin_productconfig_common', # '旗云管理员后台.产品配置.qiyun_admin_productconfig_cloudserver', # '旗云管理员后台.财务管理.qiyun_admin_financialmanage_ordermanage', # '旗云管理员后台.财务管理.qiyun_admin_financialmanage_financialmanage', -
django-celery dump celery results to view while its runnig
I have a long running celery task(10 minutes or less) and want to populate each finished result with js. While its running I want to get results when progress is still running like, def myfunc(input): res["a"] = a(input) >> get celery result res["b"] = b(res["a"]["x"]) >> get celery result res["c"] = c(input) >> get celery result res["d"] = d(res["c"]["y"] return res def getceleryresult(taskid): res = here is that i should get the unfinished tasks results return JsonResponse({'res': res}) Is there any workaround? -
Django async send notifications
I have a notifications for a users when the admin add notification to user i want to display on user's home page like a facebook likes or activity feed is it possible with django ? models.py class Notification(BaseModel): created_user = models.ForeignKey(User) title = models.CharField(max_length=225) description = models.TextField(max_length=600) is_read = models.BooleanField(default=False) priority = models.CharField(max_length=20, choices=NOTIFICATION_STATUS, default=LOW) def __str__(self): return self.title i'm able to add a new notificiation but the notifications listing when the user refresh the page. Simply i just want to do this async like facebook like system. -
Django testing: RequestFactory() follow redirect
In Django, I have a view, which will redirect to the register page for certain users and I would like to write a test for this. The standard request.client.get used for testing doesn't allow me to specify a user (it just defaults to anonymous_user?), so I can't test the behaviour. With RequestFactory() I was able to specify request.user. However, it is not following the redirect and the test fails. from .views import my_view from django.test import RequestFactory() def test_mytest(self): user = create_guest_user() self.factory = RequestFactory() request = self.factory.get(reverse('my_view'), follow=True) request.user = user response = my_view(request) self.assertContains(response, "page where redirected should contain this") It fails on the last line with this error message: AssertionError: 302 != 200 : Couldn't retrieve content: Response code was 302 (expected 200) Any ideas how to do this? -
I can't get ( GDAl or GIS, GEOS, ) to be all compatible and work together in Django framework . any alternative frameworks for geolocation WEB APP
I'm writing small project for the university, where i want to show the people who are moving in a city based in their location. I have some experience with python and therefore the prof ask me to write and create a Web APP with Django and that include API, REST elc... My problem here that I can't get ( GDAl or GIS, GEOS, ) to be all compatible and work together. any alternative frameworks for geolocation WEB APP , or suggestions, I'm using windows 10 and pycharm. (Python,3.6.3 .django 1.11.7). -
Django + Auth0 "a bytes-like object is required, not 'str'" python 3.5.2
I know there has been a bunch of "a bytes-like object is required, not 'str'" question around. but i've tried and tried and no one fixes my issues. it'll be very nice if you can help me out in my particular case. I'm trying to connect django 1.11 with Auth0 and got that error on this code def get_user_details(self, response): # Obtain JWT and the keys to validate the signature idToken = response.get('id_token') jwks = request.urlopen("https://" + self.setting('DOMAIN') + "/.well-known/jwks.json") issuer = "https://" + self.setting('DOMAIN') + "/" audience = self.setting('KEY') #CLIENT_ID payload = jwt.decode(idToken, jwks.read(), algorithms=['RS256'], audience=audience, issuer=issuer).decode('UTF-8') i have tried to use .encode('utf-8') or the b"str" or the "rb" / "wb" but still no luck. Thanks in advance for your help ---- edit ---- using payload = jwt.decode(idToken, jwks.read().encode('utf-8'), algorithms=['RS256'], audience=audience, issuer=issuer).decode('UTF-8') bring me to other error 'bytes' object has no attribute 'encode' -
how can I specifically run an order on my Django python migration list?
I have a migration list (schema / data) 001 initial 002_XX 003_YY ..... In the list I have a data migration of a model: current_product_type, created = mymodel.objects.get_or_create () then I wanted to delete a field from the model but it will have a problem in data migration level declared before because it will not find a correspondence between the model and the database. how can I then specific an order of execution of my migration list -
Is it a good idea to create custom primary key in django models?
Is it a good idea to define a primary key like this: what are the advantages and disadvantages of the approach. Its showing error "TypeError at /adminusers/userdetail/add/", 'NoneType' object is not iterable. class UserType(models.Model): user_type_id = models.AutoField(primary_key=True, unique=True) user_type_name = models.CharField(max_length=100,null=True, blank=True) user_type_desc = models.CharField(max_length=100,null=True, blank=True) created_date = models.DateTimeField(null=True) created_by = models.CharField(max_length=100,null=True, blank=True) updated_date = models.DateTimeField(null=True) updated_by = models.CharField(max_length=100,null=True, blank=True) def __unicode__(self): return self.user_type_name class UserDetail(models.Model): # user = models.OneToOneField(User, on_delete = models.CASCADE) # user_detail_id = models.AutoField(primary_key=True) # user_type = models.ForeignKey(UserType, to_field="user_type_id") user = models.OneToOneField(User, on_delete=models.CASCADE) #null=True, blank=True) user_role = models.CharField(max_length=100,null=True, blank=True) first_name = models.CharField(max_length=100,null=True, blank=True) last_name = models.CharField(max_length=100,null=True, blank=True) email = models.EmailField(max_length=100,null=True, blank=True) mobile_number = models.CharField(max_length=100,null=True, blank=True) -
Group same QuerySet elements
I have a queryset that looks like this: Order.objects.all() <QuerySet [ <Order: 2017-11-09 Apple 2 >, <Order: 2017-11-09 Banana 1>, <Order: 2017-11-09 Pears 2 >, <Order: 2017-10-01 Berry 2> ]> As you can see there are 3 orders with the same date. How can I group this order by the date, so it will become something like this: Order: 2017-11-09 Apple 2, Banana 1, Pears 2 Oerder: 2017-10-01 Berry 2 -
User Permissions settings in Django?
I have edit form with user_permissions field. In that form I show list of permissions with checkboxes. Questions: 1) My current code right know in form template show me user permissions in next format: auth | user | Can add user. How to show only human readable name of user permission (Can add user)? I was looking for AUTH_PERMISSION table in DB and such human readable name stored in name field. When I tried to use in template {{ user_permission.name }} it show me nothing. How to solve this problem? 2) How to tick user permissions (checkboxes) which user has when open edit form? Is it correct to use ModelMultipleChoiceField with user_permissions field? forms.py: class UserEditForm(UserChangeForm): class Meta: model = User exclude = ('groups',) user_permissions = forms.ModelMultipleChoiceField( Permission.objects.filter(content_type__model__in=['custom_app', 'user']), widget=forms.CheckboxSelectMultiple, ) template: <div class="list-group"> {% for user_permission in user_edit_form.user_permissions %} <li class="list-group-item"> <div class="custom-checkbox"> <label for="{{ user_permission.id_for_label }}"> {{ user_permission.tag }} <span class="cr"><i class="cr-icon glyphicon glyphicon-ok"></i></span> <strong>{{ user_permission }}</strong> </label> </div> </li> {% endfor %} </div> -
APScheduler job is not starting as scheduled
I'm trying to schedule a job to start every minute. I have the scheduler defined in a scheduler.py script: from apscheduler.schedulers.background import BackgroundScheduler from apscheduler.executors.pool import ThreadPoolExecutor, ProcessPoolExecutor executors = { 'default': ThreadPoolExecutor(10), 'processpool': ProcessPoolExecutor(5) } job_defaults = { 'coalesce': False, 'max_instances': 5 } scheduler = BackgroundScheduler(executors=executors,job_defaults=job_defaults) I initialize the schedule in the init.py of the app like this: from scheduler import scheduler scheduler.start() I want to start to schedule a job on a specific action, I do it like that: def AddJob(): dbid = repository.database.GetDbid() job_id = 'CollectData_{0}'.format(dbid) scheduler.scheduled_job(func=TestScheduler(), trigger='interval', minutes=1, id=job_id ) def TestScheduler(): for i in range(0,30): starttime = time() print "test" sleep(1.0 - ((time() - starttime) % 1.0)) First when I'm executing the AddJob() function in the python console it starts to run as expected but not in background, the console is blocked until the TestScheduler function ends after 30 seconds. I was expected it to run in the background because it's a backgroundScheduler. Secondly, the job never start again even when specifiying a reapeat interval of 1 minute. What am I missing? Thanks -
Elegant way of fetching multiple objects in custom order
What's an elegant way for fetching multiple objects in some custom order from a DB in django? For example, suppose you have a few products, each with its name, and you want to fetch three of them to display in a row on your website page, in some fixed custom order. Suppose the names of the products which you want to display are, in order: ["Milk", "Chocolate", "Juice"] One could do unordered_products = Product.objects.filter(name__in=["Milk", "Chocolate", "Juice"]) products = [ unordered_products.filter(name="Milk")[0], unordered_products.filter(name="Chocolate")[0], unordered_products.filter(name="Juice")[0], ] But is there a more elegant way? e.g., convey the desired order to the DB layer somehow, or return the products grouped by their name (aggregation seems to be similar to what I want, but I want the actual objects, not statistics about them). -
Django - get value of readonly field (fix readonly error)
I'm trying to edit fieldset.html template in order to add the hidden input under readonly div (in readonly case). {% if field.is_readonly %} <div class="readonly {% if field.field.name %} field-{{ field.field.name }}{% endif %}">{{ field.contents }}</div> <input id="{% if field.field.name %} id-{{ field.field.name }}{% endif %}" type="hidden" value="{{ field.field.initial }}"/> {% else %} {{ field.field }} {% endif %} My problem is that if I set a field readonly with "get_readonly_fields", I can't submit the form because hidden field is required (I think this is a big error of django that uses div instead of the hidden input). I tried to fix it with the code above but I'am not able to insert the value in to my field, because "field.field.initial" is empty for readonly field. How can I solve it?