Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
DefaultSocialAccountAdapter populate_user() and ForeignKeyo or OnetoOne field to default Django User model
I want to create and custom UserProfile model which is having one-to-one relation field to User model and also get populated by the social account user data. -
Nesting APIView breaks request if request.FILES property is called
We have an APIView (FooView) that can be accessed directly through a URL. We have another APIView APIKeyImportView that will reuse FooView depending of file name (it is done this way for compatibility with an API). However when request.FILES is accessed from APIKeyImportView to look at the file names, request.FILES becomes empty in FooView. It appears that accessing request.FILES will makes it un-useable by the nested view. Is there a way around this? class FooView(APIView): permission_classes = (permissions.IsAuthenticated,) def post(self, request, vendor): file = request.FILES.get('file') if not file: return Response(status=status.HTTP_400_BAD_REQUEST) return Response() class APIKeyImportView(APIView): permission_classes = (permissions.IsAuthenticated,) authentication_classes = (ApiKeyAuthentication,) def post(self, request): file = request.FILES.get('file') if not file: return Response(status=status.HTTP_400_BAD_REQUEST) name = file.name if name.startswith('FOO'): return FooView.as_view()(request=request) else: return Response(status=status.HTTP_400_BAD_REQUEST) Removing the validation on request.Files in APIKeyImportView will make it accessible in FooView but it kinds of miss the point. Inspecting request in PyCharm will also make it un-useable in FooView since the debugger will call the properties. class APIKeyImportView(APIView): permission_classes = (permissions.IsAuthenticated,) authentication_classes = (ApiKeyAuthentication,) def post(self, request): return FooView.as_view()(request=request) These solutions are not working: django modifying the request object -
Django: alternatives to count()
I have a Django app that displays paginated search results. Each page displays 20 results and I have a pagination bar at the bottom that displays the 5 pages less than and 5 pages greater than the current page (like Google). The problem is, for the pagination bar I call count() to get the total number of results so I know if there is actually 5 pages of results ahead of the current page. The problem is more general queries could take around 10 seconds to perform a count() on. I don't actually care about the exact number, since most of my users will probably never reach the end of the results. Is there any way to estimate the output of count, or more generally, estimate the number of returned results from a query? This is currently my query to get the actual results. results = Item.objects.filter(title__icontains=query).order_by('views')[offset:limit] The offset and limit variables refer to the segment of results that is shown on the current page. The only way I can see to solve my problem is to get the result segment of ~5 pages ahead and check whether it's empty. However, there are a lot of the edge cases for … -
Transient Variables in Django
Does Django allow transient variables to be declared in models.py? If so, how do I go about doing this? I cannot find any documentation on this. Any help would be appreciated. Thanks. -
Is it possible to persist a joined field in Djangos SearchVectorField?
Is it possible to persist a joined field in Djangos SearchVectorField? For example: p.search_vector = SearchVector('brand__name') p.save() raises this exception: FieldError: Joined field references are not permitted in this query -
Filtering with GenericForeignKey (organized an extra field content_type_for_filter)
Django 1.11.4 From the documentation we can understand that filters cannot be used directly with GenericForeignKey: https://docs.djangoproject.com/en/1.11/ref/contrib/contenttypes/#django.contrib.contenttypes.fields.GenericForeignKey I organized an extra field for model name. That is content_type_for_filter. The code below works fine. Could you tell me whether this is a good practice or bad? models.py class Bookmark(CommonUrlMethodsMixin, models.Model): content_type = models.ForeignKey(ContentType) content_type_for_filter = models.CharField(max_length=6, null=False, blank=True, default="") object_id = models.PositiveIntegerField() content_object = GenericForeignKey('content_type', 'object_id') user = models.ForeignKey(User, on_delete=models.CASCADE) shell Bookmark.objects.filter(user=u, content_type_for_filter="frame", object_id=1) <QuerySet [<Bookmark: Frame: 1 asdf>]> -
Spark with Django big data issue
I have a django web app that has huge amounts of data. I have tables that are 4 million records and other tables that has more than 17 million records. I'm trying to load a table which is 3480213 records in a spark dataframe to do some processing. Here is my code: >>> from accounts.models import Custom_User >>> from pyspark import SparkContext >>> sc = SparkContext("local") Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties Setting default log level to "WARN". To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel). 17/08/24 14:21:30 WARN Utils: Your hostname, ip-10-0-0-153 resolves to a loopback address: 127.0.0.1; using 10.0.0.153 instead (on interface eth0) 17/08/24 14:21:30 WARN Utils: Set SPARK_LOCAL_IP if you need to bind to another address >>> data = sc.parallelize(Custom_User.objects.all()) 17/08/24 14:42:24 WARN NettyRpcEnv: Ignored failure: java.util.concurrent.TimeoutException: Cannot receive any reply from 10.0.0.153:41444 in 10 seconds 17/08/24 14:50:50 WARN NettyRpcEnv: Ignored failure: java.util.concurrent.TimeoutException: Cannot receive any reply from 10.0.0.153:41444 in 10 seconds I'm working on an AWS EC2 instance with 8GB of RAM. Is this the problem? I thought spark optimize things internally so it doesn't eat memory. -
Requests for Instagram Access token
I am working on Instagram API in Django(python) I am getting code from 'https://api.instagram.com/oauth/authorize/?client_id=%s&response_type=code&redirect_uri=%s' % (INSTAGRAM_APP_CONSUMER_KEY, redirect_uri) but when i am exchanging code for access token code is failing # All arguments are valid def execute(self, code, redirect_uri, app_id, app_secret): exchange_url = 'https://api.instagram.com/oauth/access_token' try: r = requests.get(exchange_url, params={ 'client_id': app_id, 'redirect_uri': redirect_uri, 'client_secret': app_secret, 'code': code, 'grant_type': 'authorization_code' }) #print(r) #print(json.loads(r)) print(r.json()) return r.json() except Exception as e: print(e) r.json() gives simplejson.scanner.JSONDecodeError: Expecting value: line 1 column 1 Please let me know what i am doing wrong -
`requst.GET.get("key_here")` returns nothing in Django's view from AngularJS' `$http` [duplicate]
This question already has an answer here: AngularJS passing data to $http.get request 7 answers These are my codes. main.js $http({ data: $.param({ "test": "data" }), method: "GET", url: url // Refer to URL generated from DjangoTemplates. }).then(function(data){ console.log(data); }); views.py # Create your views here. def check_user_existence(request): print(request.GET.get("test", "")) return HttpResponse("asd") urls.py url(r"^check_user_existence/?", views.check_user_existence, name="check_user_existence"), From browser console, {data: "asd", status: 200, config: {…}, statusText: "OK", headers: ƒ, …} the results is what I expected. However, from Python console it returns nothing (""). Where I expect to have "data" printed on the console. -
Attribute error: 'User' object has no attribute 'is_admin'
I have customized user model by extending AbstractBaseUser, the user name only accepted as email id. Here is the model: class User(AbstractBaseUser, PermissionsMixin): email = models.EmailField('email address', unique=True) first_name = models.CharField('first name', max_length=30, blank=True) last_name = models.CharField('last name', max_length=30, blank=True) date_joined = models.DateTimeField('date joined', auto_now_add=True) is_active = models.BooleanField('active', default=True) is_staff = models.BooleanField(default=False) objects = UserManager() USERNAME_FIELD = 'email' REQUIRED_FIELDS = [] class Meta: verbose_name = 'user' verbose_name_plural = 'users' def get_full_name(self): full_name = '%s %s' % (self.first_name, self.last_name) return full_name.strip() def get_short_name(self): return self.first_name def email_user(self, subject, message, from_email=None, **kwargs): send_mail(subject, message, from_email, [self.email], **kwargs) The model manager for the above model is: class UserManager(BaseUserManager): use_in_migrations = True def _create_user(self, email, password, **extra_fields): """ Creates and saves a User with the given email and password. """ if not email: raise ValueError('The given email must be set') email = self.normalize_email(email) user = self.model(email=email, **extra_fields) user.set_password(password) user.save(using=self._db) return user def create_user(self, email, password=None, **extra_fields): extra_fields.setdefault('is_superuser', False) return self._create_user(email, password, **extra_fields) def create_superuser(self, email, password, **extra_fields): extra_fields.setdefault('is_superuser', True) if extra_fields.get('is_superuser') is not True: raise ValueError('Superuser must have is_superuser=True.') return self._create_user(email, password, **extra_fields) I am able to create the custom user but when I am trying to log in I got the attribute error. … -
Validate the identity of a person
I work in a programmer's team. We have created a web application in Django that manages micro-loans. One of our customers made two applications under the same name with two different social insurance numbers (NAS). Here in Canada, this number allows us to validate the identity of a person. I would like to implement code that would tell me if the social insurance number is existing and whether it belongs to the name of the person who applied for a loan. Does anyone have a solution for that kind of problem? What is the best way to implement something like this? Assume the name is John Doe, and he applied with two different NAS, i.e. 123 456 789 and 987 654 321. -
Making general Filter in Djando Rest Framework
I want to make a common filter to my models because I need to filter all my objects to return a gap between time_start and time_end, but apparently it doesn't work. I'm not sure if it's even possible(But I hope so, because it won't by DRY otherwise). models.py class Time(models.Model): time = models.TimeField() class Meta: abstract=True class Mark(Time): value = models.IntegerField(verbose_name="mark") teacher = models.CharField(max_length=20) subject = models.CharField(max_length=20) serializers.py class MarkSerializer(serializers.ModelSerializer): class Meta: model = Mark fields = ('id', 'time','value', 'teacher', 'subject') filers.py class DataFilter(django_filters.FilterSet): start_time = django_filters.TimeFilter(name="time", lookup_expr='gte') end_time = django_filters.TimeFilter(name="time", lookup_expr='lte') class Meta: model = Time fields = ['start_time', 'end_time'] views.py class MarkViewSet(viewsets.ModelViewSet): serializer_class = MarkSerializer queryset = Mark.objects.all() filter_class = DataFilter I try to get needed marks through: 127.0.0.1:8000/api/v0/marks/?time_start=11:40:00&time_end=12:00:00 but it returns all the objects that I have not the filtered ones. Thanks in advance. -
Django: Is it possible to use model's @property decorators with Model.values()?
I have a model with the following fields and decorators: class Item(models.Model): title = models.CharField(db_index=True, max_length=100) thumb = models.CharField(max_length=1000) vendor = models.CharField(max_length=15) url = models.CharField(max_length=255, unique=True) views = models.CharField(max_length=64) @property def parsed_thumb() return = self.url + "/static/" + self.thumb which I query in a class based view with: results = Items.objects.filter(title__icontains=query).order_by('-views')[offset:limit].values('id', 'url', 'title', 'vendor', 'thumb') The problem is the call to values() returns db fields and, as far as I know, has no way of interacting with @property decorators that belong to the Items model. I need the return value from parsed_thumb(). What are my options? -
Javascript + Django, simple calculation not working
I need to make a simple division between two numbers for more than 100 items in a list of variables, Inside the loop the HTML looks something like: <input id="box1{{ item.0 }}" type="text" onkeyup="calculate()" /> <input id="box2{{ item.0 }}" type="text" onkeyup="calculate()" value="{{ item.7 }}"/> <input id="result{{ item.0 }}" /> And my script function calculate(){ console.log(1); var myBox1 = document.getElementById('box1{{ item.0 }}').value; var myBox2 = document.getElementById('box2{{ item.0 }}').defaultValue; var result = document.getElementById('result{{ item.0 }}'); var myResult = myBox1 / myBox2; result.value = myResult;} For some reason I can't get this to work inside the loop, the script is inside the loop for each item in my list, maybe it is because JS can't capture the jinja variable {{ item }} ? -
Django: DetailView containing reviews. Add a form for reviews with update/create
Currently I have the following structure. I have Users, which can be Teachers or Students. Students can leave reviews for teachers. I've set up a detailed view for teachers and added a 'reviews' attribute to get_context_data to loop through reviews to display them. Aim: Each user who is a student can submit a review of a teacher. I want to display a form at the bottom of my detailed view. If user already had a review, then we call update. If user doesn't have a review, we create it. Models.py class Teacher(models.Model): user = models.OneToOneField(User, on_delete=models.PROTECT, related_name='Teacher') availability = models.BooleanField(default=False) def __str__(self): return self.user.username class Student(models.Model): user = models.OneToOneField(User, on_delete=models.PROTECT, related_name='Student') teacher = models.ForeignKey(Teacher) reviewed = models.BooleanField(default=False) def __str__(self): return self.user.username class Review(models.Model): teacher = models.ForeignKey(Teacher) student = models.OneToOneField(Student, on_delete=models.PROTECT, related_name='Student') star = models.IntegerField(default=5) body = models.TextField() Views.py class ReviewSubmitForm(forms.Form): star = forms.IntegerField() body = forms.CharField() class TeacherView(generic.DetailView): model = Teacher def get_context_data(self, **kwargs): # Call the base implementation first to get a context context = super(TeacherView, self).get_context_data(**kwargs) # Add extra context from another model context['reviews'] = Review.objects.filter(teacher_id=self.kwargs['pk']) context['rating'] = Review.objects.filter(teacher_id=self.kwargs['pk']).aggregate(Avg('star')) context['form'] = ReviewSubmitForm() return context class ReviewSubmit(SingleObjectMixin, FormView): template_name = 'users/teacher_detail.html' form_class = ReviewSubmitForm model = Review def post(self, … -
exporting one column to django database with python
I have an excel file, in which there are emails of users in column F. I would like to get the emails in each row and import into a django database. However; as I am very new to Django, I do not understand how to do it. Here is what I tried: import openpyxl from ms.common.models import User, Participant, Question, Answer wb = openpyxl.load_workbook('list.xlsx') ws = wb.get_sheet_names() for row in ws.iter_rows('F{}:F{}'.format(ws.min_row,ws.max_row)): for cell in row: print cell.value #create email participant, created = Participant.objects.get_or_create(mail=user.username) -
docker with python3 and django production image?
Does anyone use docker for a production image of Django, and how do you build that box? I'm using the base python image with a requirements.txt for development. but id like to use a docker image for production too, but appreciate a web server should be used for this. I've used apache in the past. is there a good approach to this? Thanks -
gunicorn socket is not being created - I tried everything
I tried really everything, something is messed up, my socket file is not being created. my checklist: gunicorn is running nginx is running if I start the app 0.0.0.0:8090, I can access the page in browser but just normal request not working. this is what nginx logs say: my project folder from www has 775 and www-data as user and group. These are my gunicorn settings: service: [Unit] Description=gunicorn daemon After=network.target [Service] User=www-data Group=www-data WorkingDirectory={{ app_dir }} ExecStart={{ virtualenv_dir }}/bin/gunicorn --workers 3 --bind unix:{{ app_dir }}/live.sock {{ wsgi_module }}:application [Install] WantedBy=multi-user.target and socket settings: [Unit] Description=gunicorn socket [Socket] ListenStream={{ app_dir }}/live.sock [Install] WantedBy=sockets.target nginx: upstream wab { server unix://{{ app_dir }}/live.sock; } server { listen 80; server_name .woambesten.de 78.47.110.55; location / { uwsgi_pass wab; include /etc/nginx/uwsgi_params; } } I see that it is permission issue, but where? :/ any help is appreciated the weird thing is, I see uwsgi://unix:/ in the error logs even if I am running gunicorn, is that normal? -
ModelForm not saving BooleanField default value
I have the following Model: class TransType(models.Model): typeName = models.CharField(max_length=200) isActive = models.BooleanField(default=True, blank=True) def __str__(self): return self.typeName Whenever I am creating a new object without specifying isActive value, it is saving the object with isActive set to false even though I have set the default to True. What could be the issue here? EDIT: <form method="POST" action="{% url 'trans:createmode' %}" class="modal-content form-horizontal" id="creator"> {% csrf_token %} {{ form }} <label for="mode" class="col-sm-3 control-label">Transport Mode</label> <input type="text" name="typeName" maxlength="200" class="form-control" required="required" id="typeName"> <input type="submit" class="btn btn-fill btn-success pull-right" id="submit" value="Save"> </form> -
Doubts about django-tenant-schemas SHARED AND TENANT_APPS configuration
I've read the django-tenant-schemas documentation but I still have not a clear vision about where to put each of my INSTALLED_APPS in SHARED/TENANT APPS configuration. All the apps listed in INSTALLED_APPS should be also in SHARED or TENANT lists? (django.contrib.admin, django.contrib.sessions, django.contrib.messages, ...) SHARED_APPS: tenant_schemas + customers + content_types + any other public app? TENANT_APPS: content_types + my tenant apps? I've seen examples of apps listed in both SHARED and TENANT. Why is that? To sync the database the only change is use migrate_schemas instead migrate? Are these 2 steps ok to sync the database? python manage.py makemigrations python manage.py migrate_schemas --shared -
How to pass the currently logged in user as default to a form
How do i pass the currently logged in user as default to a form? I want the form to be pre-populated with the current user, but at the same time allow the user to change it (e.g. add the Customer for another user). Here is my model: class Customer (models.Model): shortname = models.CharField(max_length=30) name = models.CharField(max_length=50, blank=True) [...] user = models.ForeignKey(User) nextstep = models.DateField(auto_now=False, auto_now_add=False, default=(date.today() + timedelta(days=10))) def due_in(self): due_time = self.nextstep - date.today() overdue = due_time.days < 0 return {'days': abs(due_time.days), 'overdue': overdue} def get_absolute_url(self): """Absolute URL des Partners""" return reverse('kundencenter:partner_detail', kwargs={'pk': self.pk}, current_app='kundencenter') And my form: class CustomerForm(forms.ModelForm): class Meta: model = Customer fields = [ "shortname", "name", [...] "user", ] labels = { "shortname": 'Customer Shortname', "name": 'Full Name', } -
EmbeddedDocument in a Document doesn't initialize
I'm using drf_mongoengine for the first time and I'm having problems setting up the models. I want the documents to be initialized like this: { "name" : "new_name", "metadata": { "total_efficiency": 0.0, "eff_vs_layer_thickness":{ "x":[], "y":[] } } } The documents are created without the "metadata" field. What Am I missing? Models: class Detector(Document): name = fields.StringField(null=True) metadata = fields.EmbeddedDocumentField(Metadata, null=False) class Metadata(EmbeddedDocument): eff_vs_layer = fields.EmbeddedDocumentField(Plot) total_efficiency = fields.DecimalField(null=True, default=0) class Plot(EmbeddedDocument): x = fields.ListField(fields.FloatField(null=True), default=[]) y = fields.ListField(fields.FloatField(null=True), default=[]) Serializer: class DetectorSerializer(mongoserializers.DocumentSerializer): class Meta: model = Detector fields = '__all__' class MetadataSerializer(mongoserializers.EmbeddedDocumentSerializer): class Meta: model = Metadata fields = '__all__' View: class DetectorViewSet(viewsets.ModelViewSet, mixins.UpdateModelMixin, mixins.DestroyModelMixin): ''' Contains information about inputs/outputs of a single program that may be used in Universe workflows. ''' lookup_field = 'id' serializer_class = DetectorSerializer -
Django FieldError: Unknown fields specified
I am getting this error django.core.exceptions.FieldError: Unknown field(s) (first_name, username, last_name) specified for SystemUser Here is my model. I extended the User model since I want to add additional fields to it. class SystemUser(models.Model): user = models.OneToOneField(User) POSITION_CHOICES = ( ('ADMIN', 'Admin'), ('SALES EMPLOYEE', 'Sales Employee'), ('INVENTORY MANAGER', 'Inventory Manager'), ('FINANCIAL OFFICER', 'Financial Officer'), ('PROJECT HEAD', 'Project Head'), ('SHIPPER', 'Shipper'), ('SUPPLIER', 'Supplier') ) position = models.CharField(choices=POSITION_CHOICES, max_length=1) Here is my forms.py class UserForm(forms.ModelForm): password = forms.CharField(widget=forms.PasswordInput) class Meta: model = SystemUser fields = ['username', 'first_name', 'last_name', 'password', 'position'] -
Post, Put and Get requests in Angular 4
I am new to Angular, but I need to create post, get request to allow registration and login in for user used API which is written in django, I can't use jwt, I am trying to understand documentation, but It is too hard for me, so maybe somebody have example or can explain how I can write request and get tokens in header from API. Please help me -
How to pass a JSON to a an Ajax request in Django?
I am trying to pass a dictionary as a Json to an Ajax request with the following code: Views.py from rest_framework.response import Response class ChartData8(APIView): def tickets_per_day_results(request): if request.method == "POST": template_name = 'personal_website/tickets_per_day_results.html' year = request.POST.get('select_year', None) week = request.POST.get('select_week', None) ....do stuff... data = {"label_number_days": label_number_days, "days_of_data": count_of_days} return render(request,template_name,{Response(data)}) Response is the class I use to convert data to a json format but then I have to wrap it as a dictionary {} so I can avoid the error context must be a dict rather than Response. template_1 $.ajax({ method: "POST", url: endpoint, dataType: 'json', contentType: "application/json", headers: {"X-CSRFToken": $.cookie("csrftoken")}, success: function(data){ console.log(data) label_number_days = data.label_number_days days_of_data = data.days_of_data setChart() }, error: function(jqXHR,error_data, errorThrown){ console.log("error on data") console.log(error_data) console.log(JSON.stringify(jqXHR)) console.log("AJAX error: " + error_data + ' : ' + errorThrown) } }) Everything works OK until the browser throws me the error AJAX error: parsererror : SyntaxError: Unexpected token < in JSON at position 0. This error is due to the fact that I am led to believe the response body is actually HTML and not Json. My main question is: How can I convert the result to a Json so it can be read smoothly by …