Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
How can I use Formset in Django Form Wizard (and change dynamically the parameter extra)
As you can see in my question, I'm trying to use a Formset in a Django Form Wizard and I would like to change the parameter "extra" depending on the previous step. The way I proceed displays de right amount of forms but it seems that only the first form is saved and validated. Here is my "relevant" code (Sorry if it's not very clear, I'm new to Django...): VIEWS.PY from django.forms import BaseFormSet HoraireFormset = formset_factory(HoraireForm,formset=BaseFormSet, extra=2) formset = HoraireFormset FORMS = [("infos_generales", InformationsGeneralesForm), ("frequence_duree", FrequenceDureeForm), ("horaire", formset), ("confirmation", ConfirmationForm) ] TEMPLATES = {"infos_generales": "inscription/infos-generales.html", "frequence_duree": "inscription/frequence-duree.html", "horaire": "inscription/horaire.html", "confirmation": "inscription/confirmation.html"} class InscriptionWizard(SessionWizardView): def get_template_names(self): return [TEMPLATES[self.steps.current]] def get_form(self, step=None, data=None, files=None): form = super(InscriptionWizard, self).get_form(step, data, files) step = step or self.steps.current if step == 'horaire': current_form = self.get_cleaned_data_for_step('frequence_duree') days = current_form['jours'] form.extra = len(days) # number of forms == number of days checked jours_de_la_semaine = ['Lundi', 'Mardi','Mercredi','Jeudi','Vendredi'] return form def get_context_data(self, form, **kwargs): context = super(InscriptionWizard, self).get_context_data(form=form, **kwargs) if self.steps.current == 'horaire': step_0_data = self.storage.get_step_data('infos_generales') club = Club.objects.get(pk = step_0_data.get('infos_generales-club')) current_form = self.get_cleaned_data_for_step('frequence_duree') days = current_form['jours'] modules = [Module.objects.filter(club=club, jour=day)[0] for day in days] context.update({'jours': modules}) return context FORMS.PY # -*- coding: utf-8 -*- from django import … -
Good rule of thumb for celery with RabbitMQ task result size?
In a Django project I've got a celery task with results of about 1 MB in size. The results are easily serializeable. In the very short term there will be just one worker running on same host, but I can see in the not-too-distant future needing workers on several machines. Currently using RabbitMQ as the result backend. I can't decide if it's smarter to write these results somewhere (database, filesystem, whatever) and have the next task that processes these results fetch them or if it's OK to return that data and let Celery pass the data to the next task. My gut tells me that there is some size that wouldn't work well with just returning the data from the task and letting Celery manage the result. Surely 1TB of data wouldn't work (well)? 1GB? 1MB? Is there a good rule of thumb for this? -
how to set Permission on a field in Django models ?
Im using Django(1.10) and Django-rest-framework. im planning to implement the ACL(access control list) on my models. I want to have some permission on my fields , not my class. For example the type1 users can see the insert_time of my object while the type2 users cannot. Remember that im using Rest. means i do not have any templates or front end, my handlers(views) and looks like services and my application gonna be a service-oriented. What is the solution ? -
Geocomplete - how to pass entire geolocator object to backend (Django)
(... and not just the name of the location.) I'm using Geocomplete with my Django project, trying to get a user's geographical information as they register. I have a User_Profile object defined like so: models.py class UserProfile(models.Model): user = models.OneToOneField(User) website = models.URLField(blank=True, null=True) location = models.CharField(max_length=200, null=True) longitude = models.FloatField(default=0.1, null=True) latitude = models.FloatField(default=0.1, null=True) def __unicode__(self): return self.user.username forms.py class UserProfileForm(forms.ModelForm): class Meta: model = UserProfile fields = [ "website", "location", ] widgets = { 'location': forms.TextInput( attrs={'id': 'location', 'required': True, 'placeholder': 'location'} ), } I'm binding the geocomplete method to the 'location' id as defined in my form widget like so: <form id="user_form" method="post" action="/register/" enctype="multipart/form-data"> {% csrf_token %} {{ user_form.as_p }} {{ profile_form.as_p }} <input type="submit" name="submit" value="register" /> </form> <script> $("#location") .geocomplete() .bind("geocode:result", function(event, result){ var locationData = result; }); $("#user_form").submit( function(){ var input = $("<input>").attr("type", "hidden").val(locationData); $("#user_form").append($(input)); console.log(toString($("#user_form"))); }); </script> When this form is submitted, I would like for the entire geolocator object returned by the geocomplete to be appended to the POST request so I can access it on the backend. However, only the 'location' (the address chosen) is sent with the post. Can anyone offer an effective way to do this? Thanks! -
How to manage a Apache Spark context in Django?
I have a Django application that interacts with a Cassandra database and I want to try using Apache Spark to run operations on this database. I have some experience with Django and Cassandra but I'm new to Apache Spark. I know that to interact with a Spark cluster first I need to create a SparkContext, something like this: from pyspark import SparkContext, SparkConf conf = SparkConf().setAppName(appName).setMaster(master) sc = SparkContext(conf=conf) My question is the following: how should I treat this context? Should I instantiate it when my application starts and let it live during it's execution or should I start a SparkContext everytime before running an operation in the cluster and then kill it when the operation finishes? Thank you in advance. -
How get sum of total values in stackedBar ChartJs
I'm trying to get the sum of all values of a stackedBar and include this total in tooltip. Note: my datasets aren't static, this is an example var barChartData = { labels: ["January", "February", "March", "April", "May", "June", "July"], datasets: [{ label: 'Corporation 1', backgroundColor: "rgba(220,220,220,0.5)", data: [50, 40, 23, 45, 67, 78, 23] }, { label: 'Corporation 2', backgroundColor: "rgba(151,187,205,0.5)", data: [50, 40, 78, 23, 23, 45, 67] }, { label: 'Corporation 3', backgroundColor: "rgba(151,187,205,0.5)", data: [50, 67, 78, 23, 40, 23, 55] }] }; window.onload = function() { var ctx = document.getElementById("canvas").getContext("2d"); window.myBar = new Chart(ctx, { type: 'bar', data: barChartData, options: { title:{ display:true, text:"Chart.js Bar Chart - Stacked" }, tooltips: { mode: 'label', callbacks: { label: function(tooltipItem, data) { var corporation = data.datasets[tooltipItem.datasetIndex].label; var valor = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index]; var total = eval(data.datasets[tooltipItem.datasetIndex].data.join("+")); return total+"--"+ corporation +": $" + valor.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,'); } } }, responsive: true, scales: { xAxes: [{ stacked: true, }], yAxes: [{ stacked: true }] } } }); }; Now total is the sum per dataset and I need the sum per stackedBar. Example Label A: value A Label B: value B Label C: value C TOTAL: value A + value B + value C … -
Override MultiWidget form in Django
I'm looking for a way to override a form from another app. The form is a MultiWidget and I would like to modify the TextInput to add a second validator on it: class KeywordsWidget(forms.MultiWidget): def __init__(self, attrs=None): """ Setup the text and hidden form field widgets. """ widgets = (forms.HiddenInput, forms.TextInput(attrs={"class": "vTextField"})) super(KeywordsWidget, self).__init__(widgets, attrs) self._ids = [] How could I do that with Django? -
Django admin change_list: Change multiple column names
I want to change multiple column names in list_display. You could do this with an attribute function using short_description as shown here: Django admin listview Customize Column Name. However, this sounds not very practical if I want to change around 25 column headers. Is there a more efficient way? -
TypeError at /add/ unhashable type: 'list'
I got the following traceback when I try to save inline formset in django: Django Version: 1.10 Python Version: 3.5.1 Installed Applications: ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'books', 'bootstrapform'] Traceback: File "D:\virtualEnv\lib\site-packages\django\core\handlers\exception.py" in inner 39. response = get_response(request) File "D:\virtualEnv\lib\site-packages\django\core\handlers\base.py" in _get_response 187. response = self.process_exception_by_middleware(e, request) File "D:\virtualEnv\lib\site-packages\django\core\handlers\base.py" in _get_response 185. response = wrapped_callback(request, *callback_args, **callback_kwargs) File "D:\virtualEnv\lib\site-packages\django\views\generic\base.py" in view 68. return self.dispatch(request, *args, **kwargs) File "D:\virtualEnv\lib\site-packages\django\views\generic\base.py" in dispatch 88. return handler(request, *args, **kwargs) File "D:\virtualEnv\inlineformset\books\views.py" in post 27. return self.form_valid(form, formset) File "D:\virtualEnv\inlineformset\books\views.py" in form_valid 52. return redirect(self.object.get_absolute_url()) File "D:\virtualEnv\lib\site-packages\django\shortcuts.py" in redirect 56. return redirect_class(resolve_url(to, *args, **kwargs)) File "D:\virtualEnv\lib\site-packages\django\shortcuts.py" in resolve_url 147. return reverse(to, args=args, kwargs=kwargs) File "D:\virtualEnv\lib\site-packages\django\urls\base.py" in reverse 91. return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))) File "D:\virtualEnv\lib\site-packages\django\urls\resolvers.py" in _reverse_with_prefix 343. possibilities = self.reverse_dict.getlist(lookup_view) File "D:\virtualEnv\lib\site-packages\django\utils\datastructures.py" in getlist 140. return super(MultiValueDict, self).__getitem__(key) My views: class FormsetMixin(object): object = None def get(self, request, *args, **kwargs): if getattr(self, 'is_update_view', False): self.object = self.get_object() form_class = self.get_form_class() form = self.get_form(form_class) formset_class = self.get_formset_class() formset = self.get_formset(formset_class) return self.render_to_response(self.get_context_data(form=form, formset=formset)) def post(self, request, *args, **kwargs): if getattr(self, 'is_update_view', False): self.object = self.get_object() form_class = self.get_form_class() form = self.get_form(form_class) formset_class = self.get_formset_class() formset = self.get_formset(formset_class) if form.is_valid() and formset.is_valid(): return self.form_valid(form, formset) … -
Access HDF files stored on s3 in pandas
I'm storing pandas data frames dumped in HDF format on S3. I'm pretty much stuck as I can't pass the file pointer, the URL, the s3 URL or a StringIO object to read_hdf. If I understand it correctly the file must be present on the filesystem. Source: https://github.com/pydata/pandas/blob/master/pandas/io/pytables.py#L315 It looks like it's implemented for CSV but not for HDF. Is there any better way to open those HDF files than copy them to the filesystem? For the record, these HDF files are being handled on a web server, that's why I don't want a local copy. If I need to stick with the local file: Is there any way to emulate that file on the filesystem (with a real path) which can be destroyed after the reading is done? I'm using Python 2.7 with Django 1.9 and pandas 0.18.1. -
Django Twitter callback goes to IP-adress, not domain
I suddenly got a problem with the twitter redirect going to the IP-adress of my site, not the actual domain, after authorization. This has been working as expected before. I am using Django for my project, and the IP-adress is in the ALLOWED_HOSTS list. The twitter application itself is correctly figured with the domain, not any IP-adress, it just "chooses" to use the IP, it seems. Have I misconfigured something server-side maybe? Im a bit of a Django newbie. Thanks for any suggestions. -
convert string from date.toString() back to Date in Python [duplicate]
This question already has an answer here: Converting string into datetime 13 answers I've a string like: Fri Sep 23 2016 00:00:00 GMT+0200 (CEST) How do I convert it back into a date object? -
iPython with Django : CommandError: No notebook (Python) kernel specs found
I'm new to jupyter/ipython and django. I have Django 1.9.5 installed on Ubuntu 16.04 and my application is running fine. I want to configure iPython(version 5.1.0) to be used with my Django application. Basically trying to call the iPython notebook (browser) from django which later can be used extensively in the application through a user interface. When I try to run the command below I get an error: $./manage.py shell_plus --notebook [W 13:46:39.247 NotebookApp] Unrecognized JSON config file version, assuming version 1 [I 13:46:41.082 NotebookApp] [nb_conda_kernels] enabled, 2 kernels found [I 13:46:41.114 NotebookApp] Writing notebook server cookie secret to /run/user/1000/jupyter/notebook_cookie_secret [I 13:46:41.399 NotebookApp] ✓ nbpresent HTML export ENABLED [W 13:46:41.399 NotebookApp] ✗ nbpresent PDF export DISABLED: No module named nbbrowserpdf.exporters.pdf [I 13:46:41.409 NotebookApp] [nb_conda] enabled [I 13:46:41.507 NotebookApp] [nb_anacondacloud] enabled CommandError: No notebook (Python) kernel specs found To resolve this I tried: ipython2 kernelspec install-self --user Under home directory .local/share/jupyter/kernels folder there is a 'kernel.json' file I'm not sure why I'm still getting the same error. Any suggestion will be very helpful in resolving this issue. Thank you. To achieve launching iPython notebook from Django I tried the following: Installed the django-extentions: pip install django-extensions In django settings.py file added … -
Complex aggregation methods as single param in query string
I’m trying to design a flexible API with django REST. What I meant by this is to have basically any field filterable through a query string and in addition to that have a param in the query string that can denote some complex method to perform. Ok, here are the details: views.py class StarsModelList(generics.ListAPIView): queryset = StarsModel.objects.all() serializer_class = StarsModelSerializer filter_class = StarsModelFilter serializers.py class StarsModelSerializer(DynamicFieldsMixin, serializers.ModelSerializer): class Meta: model = StarsModel fields = '__all__' mixins.py class DynamicFieldsMixin(object): def __init__(self, *args, **kwargs): super(DynamicFieldsMixin, self).__init__(*args, **kwargs) fields = self.context['request'].query_params.get('fields') if fields: fields = fields.split(',') # Drop any fields that are not specified in the `fields` argument. allowed = set(fields) existing = set(self.fields.keys()) for field_name in existing - allowed: self.fields.pop(field_name) filters.py class CSVFilter(django_filters.Filter): def filter(self, qs, value): return super(CSVFilter, self).filter(qs, django_filters.fields.Lookup(value.split(u","), "in")) class StarsModelFilter(django_filters.FilterSet): id = CSVFilter(name='id') class Meta: model = StarsModel fields = ['id',] urls.py url(r’^/stars/$’, MyModelList.as_view()) this give me the ability to construct query strings like so: /api/stars/?id=1,2,3&fields=type,age,magnetic_field,mass this is great I like this functionality, but there are also many custom aggregation/transformation methods that need to be applied to this data. What I would like to do is have an agg= param like so: /api/stars/?id=1,2,3&fields=type,age,magnetic_field,mass,&agg=complex_method or just: /api/stars/?agg=complex_method where defining … -
Create Table from filtered data with django
I'm new in Django. I would like to create a new table called Dynamic_Ranging and I'd like to insert in this table the row retrieved from another table called Data_Ranging using the filter. Is that possible? I've read a lot of documentation but I don't understand how can I do that. Models: class Data_Ranging(models.Model): device_Id = models.IntegerField() range = models.DecimalField(max_digits=20,decimal_places=3) time = models.DateTimeField(auto_now_add=True) timestamp = models.DecimalField(max_digits=20,decimal_places=3) def __unicode__(self): return u"%d %f %f" % (self.device_Id,self.range,self.timestamp) class Meta: verbose_name_plural = "Data_Ranging" class Dynamic_Ranging(models.Model): device_Id = models.IntegerField(primary_key=True) range = models.DecimalField(max_digits=20,decimal_places=3) time = models.DateTimeField(auto_now_add=True) timestamp = models.DecimalField(max_digits=20,decimal_places=3) def __unicode__(self): return u"%d %f %s %f" % (self.device_Id,self.range,self.time,self.timestamp) class Meta: verbose_name_plural = "Dynamic_Ranging" Views: @csrf_exempt def Data_ranging(request): print request.POST Timestamp = request.POST['timestamp'] Device_Id = request.POST['raspNumber'] Range = request.POST['ranging'] ranging_obj = Data_Ranging(device_Id=Device_Id,range=Range,timestamp=Timestamp) ranging_obj.save() return HttpResponse("Data_Ranging") @csrf_exempt def Dynamic_ranging(request): time_threshold = datetime.now() - timedelta(seconds=5) results = Data_Ranging.objects.filter(time__gte=time_threshold) print results return HttpResponse("Dynamic_Ranging") -
Django template nested variable call
Trying to mix different models information by adding to context the information from secon model view.py def get_context_data(self, **kwargs): context = super(SceneSelectListView, self).get_context_data(**kwargs) dev = Board.objects.get(address=self.kwargs['address']) for x in range(0,15): scena='S{}'.format(x) context[scena] = getattr(dev, scena) return context then display both data in the the template : <select id="scena_sel"> {% for scena in object_list %} <option value=" {{ scena.numero }}" >{{ scena.numero }}.{{ scena.nome }} - {% with scena.numero as mycontextvar %} {{ mycontextvar }} {% endwith %} {{ S3 }} </option> {% empty %} <li>No scene.</li> {% endfor %} </select > my problem is that this will return scena.numero not the context['scena.numero']: {% with scena.numero as mycontextvar %} {{ mycontextvar }} {% endwith %} how can be called the context['scena.numero']? -
Django admin page
I am getting the following error while entering username and password in Django administration. Please enter the correct username and password for a staff account. Note that both fields may be case-sensitive. -
Divide queryset on 'read' / 'unread' in Django
I'm trying to create badge flag with status 'read'/ 'unread' depending on users action (request). Models class Post(models.Model): title = models.CharField(max_length=120) body = models.TextField() def get_absolute_url(self): return "/%s" %(self.id) ... class Comment(models.Model): post = models.ForeignKey(Post) ... View def list(request): qs_posts = Post.object.all() ... Template {% for post in qs_posts %} <a href="{{ post.get_absolute_url }}" class="btn btn-primary">View</a> <p>{{ post.body }}</p> <p> all messages: <span class="badge">{{ post.comment_set.count }}</span></p> <p> unread messages: <span class="badge red">{{ post.comment_set.count }}</span></p> {% endfor %} I'm trying to display count of 'unread'. Don't need to display it for specific user just for any request. -
retrieving objects from another model
i have this situation: class Item (models.Model): name = models.Charfield(MaxLenght=15) class ItemIsHired(models.Model): item = models.ForeignKey(Item) isHiredNow = models.BooleanField(null=False) the thing is that i want to retrieve all the Items that are hired right now, but if i make a ItemIsHired.objects.filter(isHiredNow=True) i will get objects from the ItemIsHired class when i want the objects from the Item class related to them. -
Python IF conditions
I'm new to django python so the if makes me feel confused. Please help me! Im trying to make a IF condition for Facebook chat bot API. The whole function is when a user sends something in the Facebook chat, my bot will remove all punctuations, lower case the text and split it based on space. After that he will pick a joke from json list with the keyword he got. A quick reply with 2 buttons "Yes" and "No" is also sent to user with the joke. It also checks if the keyword is absent. Now I want that when user types "No" in the chat, my bot will send something back like "Then not". What did I do wrong here? strong texttokens = re.sub(r"[^a-zA-Z0-9\s]", ' ', recevied_message).lower().split() joke_text = '' for token in tokens: if token in jokes: joke_text = random.choice(jokes[token]) .... some code send_message(fbid, joke_text) quick_text = "Do you want another joke? " send_quick_reply_message(fbid, quick_text) break if not joke_text: joke_text = "I didn't understand! " \ "Send 'stupid', 'fat', 'dumb' for a Yo Mama joke!" send_message(fbid, joke_text) if 'No': joke_text = "Then not" send_message(fbid, joke_text) -
get() returned more than one AccessToken
I am facing 2 issues here. 1) If user has not old token, a new one should be generated and logged in but it is not happening so. 2)A newly created user when logs in gets 'You have multiple authentication backends configured and therefore must provide the backend argument or set the backend attribute on the user.' 3)old user when trying to login gets an error of get() returned more than one AccessToken -- it returned 8! Expert might understand from my code class UserLoginAPI(APIView): permission_classes = [AllowAny] serializer_class = UserLoginSerializer def post(self, request, *args, **kwargs): access_token = request.GET.get('access_token') data = request.data print('data',data) serializer = UserLoginSerializer(data=data) if serializer.is_valid(raise_exception=True): new_data = serializer.data if new_data: app = Application.objects.get(name="Foodie") try: user = User.objects.get(username=data['username']) print ('user',user) except ObjectDoesNotExist: return HttpResponse("Can't find this user") else: try: access_token = AccessToken.objects.get(user=user) except ObjectDoesNotExist: return HttpResponse('Have not set any token') else: access_token.delete() new_token = generate_token() print('new_token',new_token) AccessToken.objects.create(user=user, application=app, expires=datetime.now() + timedelta(days=365),token=new_token) print('aceess',AccessToken) login(request, user) return Response(new_data, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) serializers.py class UserLoginSerializer(ModelSerializer): username = CharField() class Meta: model = User fields = ['username', 'password'] class UserCreateSerializer(ModelSerializer): class Meta: model = User extra_kwargs = {"password": {"write_only": True}} def create(self, validated_data): user_obj = User( username = username, first_name = … -
How can I add a JOIN and a GROUP BY with an unnested array to a Django ORM query?
In an existing system, I'm often using the Django values() followed by annotate() pattern to effect an SQL GROUP BY. However, for a new feature, I need to join the whole query with an additional array, and I need to GROUP BY the elements of that additional array. In PostgreSQL, it looks something like this (greatly simplified): SELECT COUNT(ID) FROM some_table INNER JOIN ... INNER JOIN (SELECT unnest(ARRAY['A', 'B', 'C']) AS name) tag ON some_table.t LIKE tag.name || '%' GROUP BY tag.name; I would like to stay in ORM-land, because the query goes through a whole sequence of follow-up transformations which I would like to keep consistent. Is there a way in which I can 1) join with the runtime-defined (different for every query) array and 2) add it to the existing GROUP BY sequence? -
Django-Push-Notifications [Errno 101] Network is unreachable
I'm running django rest framework, and now I installed the django-push-notifications Im running on Vagrant project. I could registar devices but now trying to send a simple notification i got this : File "/home/vagrant/virtualenvs/****/lib/python3.4/site-packages/push_notifications/apns.py", line 221, in apns_send_bulk_message with closing(_apns_create_socket_to_push()) as socket: File "/home/vagrant/virtualenvs/******/lib/python3.4/site-packages/push_notifications/apns.py", line 58, in _apns_create_socket_to_push return _apns_create_socket((SETTINGS["APNS_HOST"], SETTINGS["APNS_PORT"])) File "/home/vagrant/virtualenvs/******/lib/python3.4/site-packages/push_notifications/apns.py", line 52, in _apns_create_socket sock.connect(address_tuple) File "/usr/lib/python3.4/ssl.py", line 864, in connect self._real_connect(addr, False) File "/usr/lib/python3.4/ssl.py", line 851, in _real_connect socket.connect(self, addr) OSError: [Errno 101] Network is unreachable Is some problem with network? Can someone give me some tips? Thanks -
Override get_instance import-export django
I have a basic problem in Django/ Python. But I don't know the right answer. I have to override "get_instance" in django-import-export.instance_loaders Actually i change the code directly in this function. I know this is not very clever. But I don#t understand where I should override this function. In MyModelResource or where? Hope anybody can help me. Thanks -
when to use fabric or ansible?
OVERVIEW I'd like to have reliable django deployments and I think I'm not following the best practices here. Till now I've been using fabric as a configuration management tool in order to deploy my django sites but I'm not sure that's the best way to go. In the high performance django book there is a warning which says: Fabric is not a configuration management tool. Trying to use it as one will ultimately cause you heartache and pain. Fabric is an excellent choice for executing scripts in one or more remote systems, but that's only a small piece of the puzzle. Don't reinvent the wheel by building your own configuration management system on top of fabric So, I've decided I want to learn ansible. QUESTIONS Does it make sense using both fabric and ansible tools somehow? Is it possible to use ansible from my windows development environment to deploy to production centos(6/7) servers? There is this nice site https://galaxy.ansible.com/ which contains a lot of playbooks, any good recommendation to deploy django on centos servers?