Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
How to change Representation of Many to Many related object in Django Rest Framework
I want the complete related model on GET and use the id's on CREATE, UPDATE and DELETE. I try to use to_representation. So i want to create an array of dicts called users which should show the complete users. But i get the error "unhashable type: 'ReturnDict'" when i add the dict in the object, it works fine if i would do it for a single user by writing to the array directly. class CompanySerializer(serializers.ModelSerializer): #users = UserSerializer(many=True) created_at = serializers.DateTimeField() updated_at = serializers.DateTimeField() class Meta: model = Company fields = ['id', 'name', 'street', 'city', 'postal_code', 'state', 'company_form', 'users', 'created_at', 'updated_at'] def to_representation(self, instance): representation = super(CompanySerializer, self).to_representation(instance) representation['company_form'] = CompanyFormSerializer(instance.company_form).data representation['users'] = [] for entry in instance.users.all(): user = {UserSerializer(entry).data} representation['users'].extend(user) return representation -
invalid literal for int() with base 10: 'string name'
I am making an API module which saves weather data to my Django database by running a single Django management command, which retries all the data from a source API. I've created a model 'weather data' which has all the required datatypes. I've a management command written which directly saves data to my database. The snippet of management command and models.py is shown below. def handle(self,*args,**kwargs): for city in input_file: city_name = city.strip() print(city_name) full_api_url = api + city_name + '&mode=json&units=' + unit + '&APPID=' + user_api full_wet_url = weather_api + city_name + '&mode=json&units=' + unit + '&APPID=' + user_api try: data = requests.get(full_api_url).json() dta = requests.get(full_wet_url).json() city_id = dta["id"] longitude = dta["coord"]["lon"] latitude= dta["coord"]["lat"] for dt in data["list"]: temp = dt["main"]["temp"] temp_min = dt["main"]["temp_min"] temp_max = dt["main"]["temp_max"] pressure = dt["main"]["pressure"] sea_level = dt["main"]["sea_level"] grnd_level = dt["main"]["grnd_level"] humidity = dt["main"]["humidity"] weather = dt["weather"][0] main = weather["main"] description = weather["description"] clouds = dt["clouds"]["all"] wind_speed = dt["wind"]["speed"] wind_deg = dt["wind"]["deg"] dt_txt = dt["dt_txt"] wd = weatherdata(city_name,city_id,latitude,longitude,dt_txt,temp,temp_min,temp_max,pressure,sea_level,grnd_level,humidity,main,description,clouds,wind_speed,wind_deg).save() print ("Success") except Exception as e: print (e) pass class weatherdata(models.Model): city_name = models.CharField(max_length = 80) city_id = models.IntegerField(default=0) latitude = models.FloatField(null=True , blank=True) longitude = models.FloatField(null=True , blank=True) dt_txt = models.DateTimeField() temp = models.FloatField(null = … -
Build a custom View to give permissions to a specific User (not using ADMIN)
I want to build a custom view that gives ( or take depending on the situation) permission(s) to user. What I want to do is to produce a form with all the permissions listed with check boxes next to each permission, checked already against a permission given to user. In simpler words, I want to make the customised version of Django Admin where I can give or take back the permissions. How can I do this? I can get a list of permissions by using from django.contrib.auth.models import Permission per=Permission.objects.all() similarly I can get the user object by using user=User.objects.get(id=id) model. But how can I produce a form with which has check boxes and a view to connect all the checked/unchecked permissions to the user? -
Delete operation finding a relationship that doesn't exist
In my multi-tenant site, I have the following database structure: Public (shared): tenants domains users Test: users clients projects projectusers tasks Test2: users clients projects projectusers tasks This is a simplified list - all three schemas include (among others) auth_permissions, auth_group, and auth_group_permissions. The projectusers table (cross-ref that ties users to projects), has a One-to-One field for the User table, and a ForeignKey to the projects table, as follows: class ProjectUser(DateFieldsModelMixin, models.Model): user = models.OneToOneField( User, on_delete=models.PROTECT, verbose_name=_('User'), related_name='user_projects', ) project = models.ForeignKey( Project, on_delete=models.PROTECT, verbose_name=_('Project'), related_name='project_users', ) ...other fields omitted... The point is, the reference from projectusers to users is defined in the ProjectUser model. There's no reference from User model back to ProjectUser. I wrote a load_fixtures (management) command to, well, load fixtures to set up our dev site(s) with the data we'd need to do preliminary testing (before writing/running unit-tests). One of the things that command does is empty the auth_permissions table, reset the auto-increment value back to 1, and then import its fixture so that all IDs match across local dev sites, and (eventually) in production. This is necessary because fixtures for auth_group and auth_group_permissions reference permissions by ID. The error I'm getting occurs during the … -
Django REST framework - reverse ForeignKey relations
I have the following three models structured around the premise of the Survey. class Survey(models.Model): ... id = models.UUIDField(_('Id'), primary_key=True, default=uuid.uuid4, editable=False,) name = models.CharField(_('Name'), max_length=120, blank=True, unique=True) slug = models.SlugField(_('Slug'), max_length=120, blank=True, unique=True) description = models.TextField(_('Description'), blank=True) ... Each Survey can have multiple questions SurveyQuestion: class SurveyQuestion(models.Model): ... survey = models.ForeignKey('surveys.Survey', on_delete=models.CASCADE, null=True, blank=True,) And each SurveyQuestion can have multiple answers SurveyQuestionAnswer: class SurveyQuestionAnswer(models.Model): ... survey_question = models.ForeignKey('surveys.SurveyQuestion', on_delete=models.CASCADE, null=True, blank=True,) For the sake of brevity, imagine my Survey serializers as being as simple as possible: class SurveySerialializer(serializers.ModelSerializer): class Meta: model = Survey fields = ('__all__') Effectively, what I have is the following: class Survey(APIView): """ Survey GET request endpoint: fetches Survey """ permission_classes = User def get(self, request, survey_slug): survey = Survey.objects.get(slug=survey_slug) serializer = SurveySerializer(survey) response = get_hug_response(message='Organisation Active Survey Fetched Successfully', data=serializer.data) return Response(data=response, status=status.HTTP_200_OK) But, as you could all probably tell, the corresponding surveys.get('slug') fetch only returns the fields in the Survey model. Ideally, I would like to have some sort of fetch for each SurveyQuestion, and within that nested the SurveyQuestionAnswers Any pro-tips and pointers would be most appreciated. I have tried a few things, that only throw errors. I'm struggling to know what this … -
Initialize aditional object in DJango model instance and pass model instance
Some calculations became too complex to maintain inside model so i decided to move them out and break the code in several classes and modules. There's single class serving as a facade which i would like to have available in model instance to pass data to it. It is being constructed from model instance: class Calculator: def __init__(self, field1: date, field2: float): self.field1= field1 self.field2 = field2 @classmethod def from_django_model(cls, django_model_instance): field1 = django_model_instance.field1 field2 = float(django_model_instance.field2) Currently I call it inside each property on my model like so: class DjangoModel(models.Model): # initialize the calculator def calculator(self, group): return calculator.Calculator(self) # use it @cached_property def calculated_field(self): try: return self.calculator().calculation_method except AttributeError: return "Field not set!" I feel this is not a good solutions, since now on multiple methods I'm initializing calculator object multiple times. I would like to construct it once when model is initialized and then pass it model instance. Tried doing it with model manager, but model instance is not available with it. -
How to show balance in html to users who are registered users? [duplicate]
This question already has an answer here: How to show every user specific payment for them in django? 1 answer I have assigned some random numbers as a balance to every user who sign up but I am not able to show them in html page. How to do it? Maybe I have made some mistakes in python files also. I have added those balance(numbers) in my database. So please let me know. models.py class Payment(models.Model): payment_numbers = models.CharField(max_length=100) owner = models.ForeignKey(User, on_delete=models.CASCADE) views.py def payment(request): receiving1 = Payment.objects.filter(owner=request.user) for field in receiving1: field.payment_numbers context = { 'receiving1': receiving1 } return render(request, 'index.html', context) HTML page {% for numbers1 in receiving1 %} {% if numbers1 %} <li style="float: right;">Your Balance: Rs. {{numbers1.payment_numbers}}</li> {% endif %} {% endfor %} Python Shell >>> from users.models import Payment >>> Payment.objects.all() <QuerySet [<Payment: Payment object (1)>, <Payment: Payment object (2)>]> -
Duplicate client and process creating when selecting the number of process in django
This is my html file in this html I get the saved client and process list in DB, At that time got duplication. html <tbody> {% for object in emp_obj %} <tr> <td> {{ object.process.client }}<br /> </td> <td> {% for obj in emp_obj %} {% if obj.process.client == object.process.client %} {{ obj.process.process }}<br /> {% endif %} {% endfor %} </td> </tr> {% endfor %} </tbody> In views.py I fetch the Emp_Process list to based on the Emp_Profile ID and pass the emp_object to template views.py def Emp_CP_View(request, id): emp = Emp_Profile.objects.get(pk=id) print(emp) emp_obj = Emp_Process.objects.filter(username = emp) print(emp_obj) return render(request, 'employee_view.html',{'emp_obj' : emp_obj}) The below image is i got the duplication of the data -
How to send a HTML e-mail using Wagtail editor and Wagtail-Streamfield?
I want to integrate the functionality to generate a HTML site using Wagtail tools such as its editor and the Wagtail-Streamfield and send it as HTML email. Is there a straightforward way to render the Wagtail Page as html and send it using the Django email module? -
How to convert the numpy array with the wav file without writing to the disk?
sending usertext from web to tacotron create tts - > nunpy array - > file.wav ( x ) 3 i want create tts -> numpy array - > ( dont save ) -> playable data -> web playing !! def son_vo(get_text,get_speaker): global mySyns makedirs("logdir-tacotron2/generate") checkpoint_step = None num_speakers = 2 load_path = 'logdir-tacotron2/+_2019-08-05_08-05-17/' syn_speaker_id = 1 sample_path = "logdir-tacotron2/generate" audio = mySyns.synthesize(texts=[get_text],base_path=sample_path,speaker_ids=[get_speaker],attention_trim=True,base_alignment_path=None,isKorean=True)[0] print(type(audio)) return audio import synthesizer as syn def tts(request): output = tts2(request.GET['Text'],request.GET['speaker']) return HttpResponse(output) def tts2(userText,userVoice): data = syn.son_vo(userText,userVoice) return data -
Database normalization in django
I need an optimally normalized db structure to achieve the following req. models.py class Learning_Institute(models.Model): name = models.TextField() user = models.ManyToManyField(settings.AUTH_USER_MODEL) class Course(models.Model): title = models.CharField(max_length=50) instructor = models.ForeignKey(User, on_delete=models.PROTECT, related_name='courses_taught') institute = models.ForeignKey(Learning_Institute, on_delete=models.PROTECT, related_name='courses') I need the instructor field in the Course Table to be limited to the set of users in Learning_Institute instead of all the users int the system. How do i achieve this in the DB level? -
Django Model.objects.all() returning empty QuerySet in celery task
project/project/settings.py ... CELERY_BEAT_SCHEDULE = { 'find-subdomains': { 'task': 'subdiscovery.tasks.find_subdomains', 'schedule': 10.0 } } project/subdiscovery/tasks.py from __future__ import absolute_import, unicode_literals from celery import shared_task from subdiscovery.models import Domain @shared_task def test(): print(Domain.objects.all()) return 99 The celery worker shows an empty QuerySet: celery_worker_1 | [2019-08-12 07:07:44,229: WARNING/ForkPoolWorker-2] <QuerySet []> celery_worker_1 | [2019-08-12 07:07:44,229: INFO/ForkPoolWorker-2] Task subdiscovery.tasks.find_subdomains[60c59024-cd19-4ce9-ae69-782a3a81351b] succeeded in 0.004897953000181587s: 99 However, importing the same model works in a python shell: ./manage.py shell >>> from subdiscovery.models import Domain >>> Domain.objects.all() <QuerySet [<Domain: example1.com>, <Domain: example2.com>, <Domain: example3.com>]> -
How to handle login + FB login at the same time with django-allauth module?
Well.. I started to create simple app. Following official doc of Django, I created auth logic in separate app with name users, like this: users/urls.py: from django.urls import path, re_path, include from . import views urlpatterns = [ path('', include('django.contrib.auth.urls')), path('profile/', views.redirect_to_user_profile, name='redirect-user-profile'), re_path('profile/(?P<pk>\d+)/', views.UserProfileView.as_view(), name='user-profile'), path('register/', views.UserRegisterView.as_view(), name='user-register'), users/views.py: from django.shortcuts import render from django.http import HttpResponseRedirect from django.views import generic from django.contrib.auth.models import User from django.contrib.auth.forms import UserCreationForm # Create your views here. def redirect_to_user_profile(request): url = f'/users/profile/{request.user.id}' return HttpResponseRedirect(redirect_to=url) class UserProfileView(generic.DetailView): model = User template_name = 'user_profile.html' class UserRegisterView(generic.CreateView): form_class = UserCreationForm template_name = 'register.html' success_url = '/users/login' Everything was fine, so I decided to extent basic Django user, to add profile image for example (and more fields later) like this: users/models.py: from django.db import models from django.contrib.auth.models import User from django.db.models.signals import post_save from django.dispatch import receiver # Create your models here. class ProfileUser(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) profile_image = models.URLField() @receiver(post_save, sender=User) # Still don't know how, but next rows create ProfileUser when User is created def create_user_profile(sender, instance, created, **kwargs): if created: ProfileUser.objects.create(user=instance) @receiver(post_save, sender=User) def save_user_profile(sender, instance, **kwargs): instance.profileuser.save() def __str__(self): return f"{self.user}" Still working fine. Then I decided to add FB login, … -
How to "get" User that don't sign in yet?
I'm working on email activation, I do not want the user to login before they activate their email, so I set the active status as "false". The question is how do I get that user and make it True when they activation email. def activation_view(request, activation_key): if (SHA1_RE.search(activation_key)): print("acti is real") try: instance = EmailConfirmed.objects.get(activation_key=activation_key) except EmailConfirmed.DoesNotExist: instance = None raise Http404 if instance is not None and not instance.confirmed: page_message = ("ยืนยัน User เรียบร้อย") instance.comfirmed = True instance.activation_key = "Confirmed" instance.save() user.is_active = True? elif instance is not None and instance.comfirmed: page_message = ("User ถูกยืนยันไปแล้ว") else: page_message = "" context = {"page_message":page_message} return render(request, 'accounts/activation_complete.html',context) else: raise Http404 -
In Django REST Framework, how to get the "query parameter" sent by the previous page's hidden input?
I have an html page for listing the model "Descriptions", and at the end of it there's a button to go to the Description creation page, while sending the "character_id" that is intended to be a default initial value for the new Description (and I have set up the context so that the character_id would be there: <!--The code for listing the Descriptions--> <form action="{% url 'polls:description_detail_create_from_character' %}"> <input type="hidden" value="{{ serializer.context.character_id }}" name="character_id"> <input type="submit" value="New Description"/> </form> On the browser, if I click "New Description", it will bring me to: http://localhost:8000/polls/description_detail_create_from_character/?character_id=3 However, then I don't know how can I get this "character_id" from description_detail_create_from_character (the next page)'s template. Thought it could be request.query_params.get('character_id', None), but doesn't work. By debugging, I can find the dict query_params, however, there's nothing in it. Just don't know how can I get this character_id=3. It's nowhere to be found in the {% debug %} either. Is something more to be done in the Serializer? Or View? Is this ?character_id=3 here actually a query parameter here? If it is not then what it is? Code: Serializers.py: # The serializer for the creation page class DescriptionCreateFromCharacterSerializer(DescriptionSerializer): author = serializers.HiddenField(default=serializers.CreateOnlyDefault(DefaultFieldCurrentUser())) # This works btw, unlike the … -
how to set cache in django i am not sure its saving in cache or not?
i want to reload a page fast the data coming from database takes time so i want to save in cache so for first time it will take time and then it will be store in redis server so this will help me with fast loading in data... what will be the easy way to do it and make database fast i already done indexing using PostgreSQL def render_upcoming_products(request, *args, **kwargs): if 'products' in cache: products = cache.get('products') return HttpResponse(render(request, "byond3/upcoming-trips/cards.html", context=products)) else: logger.info("Not there in cache") product_ids = [] destination = request.POST.getlist('destination[]','') duration = request.POST.getlist('duration[]','') month = request.POST.getlist('month[]','') style = request.POST.getlist('style[]','') datetime.datetime.strptime('Dec', '%b').month if destination: kwargs['destination__name__in'] = destination if style: kwargs['category__category_name__in'] = style if month: decoded_month = [datetime.datetime.strptime(m, '%b').month for m in month] kwargs['travel_planners__trip_start_date__month__in']=decoded_month else : decoded_month = [] kwargs['travel_planners__trip_start_date__gt'] = datetime.date.today().strftime('%Y-%m-%d') kwargs['is_upcomming_trip'] = True kwargs['published'] = True dictionary=dict() products = [] actions = [] kwargs1 = {} if duration: for dur_ in duration: if '>' in dur_: products.append(Product.objects.filter(Q(travel_planners__duration__gt=int(dur_.replace(' days','').replace('>',''))), **kwargs).order_by('travel_planners__trip_start_date')) elif '-' in dur_: products.append(Product.objects.filter(Q(travel_planners__duration__lte=int(dur_.replace(' days','').replace('-',',').split(',')[1]))&Q(travel_planners__duration__gte=int(dur_.replace(' days','').replace('-',',').split(',')[0])),**kwargs).order_by('travel_planners__trip_start_date')) else: products.append(Product.objects.filter(**kwargs).order_by('travel_planners__trip_start_date')) products = [j for i in products for j in i] products = list(set(products)) products.sort(key=lambda x: x.travel_planners.first().trip_start_date, reverse=False) no_of_products = len(products) no_of_dep = 0 for prod in products: no_of_dep … -
how to return a failure message when the email isn't sent with django-rest-auth?
I've set up django-rest-auth to send a password reset email. Unfortunately, the API returns a success message "Password reset e-mail has been sent" even there is no user with that email address.How can i solve this ?I want to display some messages like No email found if the entered email isn't valid. Any help would be great. -
To display the LinkedIn profile picture in Django
I am able to retrieve the profile picture from LinkedIn with social auth app django but how can I display the picture in Django? The retrieved data shows like ..."profile_picture": {"displayImage": "urn:li:digitalmediaAsset:xxxxxx"}... Is there a way to process this without using an API? -
Is it possible post the point dataset through form on django?
I want to post the point dataset into my postgres database using form. Is this possible? If yes, How can be possible? my views.py file from django.shortcuts import render from issue.models import Issue # Create your views here. def index(request): return render(request, 'pages/index.html') def about(request): return render(request, 'pages/about.html') def webmap(request): if request.method == 'POST': first_name = request.POST['firstname'] last_name = request.POST['lastname'] issue_header = request.POST['issue_header'] issue_body = request.POST['issue_body'] issue = Issue(first_name=first_name, last_name=last_name, issue_header=issue_header, issue_body=issue_body) issue.save() return render(request, 'pages/webmap.html') return render(request, 'pages/webmap.html') In this form I want to submit point data. I am using leaflet. And my marker location is var useMarker = true $('.fa-cubes').click(function () { if (useMarker) { map.on('click', function (e) { var popup = `<!-- Default form register --> <form class="text-center border border-light p-5" action="{% url 'webmap' %}" method="POST"> {% csrf_token %} <p class="h4 mb-4">Issue Form</p> <div class="form-row mb-4"> <div class="col"> <!-- First name --> <input type="text" name="firstname" id="defaultRegisterFormFirstName" class="form-control" placeholder="Your First Name"> </div> <div class="col"> <!-- Last name --> <input type="text" name="lastname" id="defaultRegisterFormLastName" class="form-control" placeholder="Your Last Name"> </div> </div> <!-- Issue header --> <input type="text" name="issue_header" id="defaultRegisterPhonePassword" class="form-control mb-2" placeholder="Input your issue header" aria-describedby="defaultRegisterFormPhoneHelpBlock"> <br> <!-- Password --> <input type="text" name="issue_body" id="defaultRegisterFormPassword" class="form-control" placeholder="Your issue in detail" aria-describedby="defaultRegisterFormPasswordHelpBlock"> <!-- … -
How to display name instead of email address while sending email in django rest framework?
This is django rest framework and here i am trying to send email and it is working fine also but the one problem is i want to display some name instead of email address in the users inbox for that i tried adding from_email argument in the urls but it didn't worked.It says TypeError: TemplateView() received an invalid keyword 'from_email'. How can i solve this settings.py EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' EMAIL_USE_TLS = True EMAIL_HOST = 'smtp.gmail.com' EMAIL_HOST_USER = "myemail" EMAIL_HOST_PASSWORD = 'mypass' EMAIL_PORT = '587' urls.py path( 'password-reset/confirm/<uidb64>/<token>/', TemplateView.as_view(template_name="registration/password_reset_confirm.html", from_email='MyName<myemail>'), name='password_reset_confirm'), -
Can i deploy both Flask and Django application in common aws Elastic Beanstalk server?
I have two applications built with Flask and Django framework. I would like to host both the applcation in aws Elastic Beanstalk. Is it possible to host both application using the common server? Thanks for the Answers. -
How can I escape colons in Django's field lookups?
I've brought some JSON metadata into a JSONfield() and some of the key names include a colon. Am I able to escape the field lookups so I can do something like the example below? filtered_qs = queryset.filter(data__properties__object:key="some_value") where object:key is the name of my JSON key Currently i'm getting the keyword cannot be an expression syntax error. I'm using Postgres 11.2 and Django 2.2.2. -
Graphene-Django Relay Cursor Based Pagination Does Not Work for Dynamic Data
I am fetching dynamic data using Graphene-Django Relay specification. import graphene from graphene_django.types import DjangoObjectType from graphene_django.fields import DjangoConnectionField from . import models class PostType(DjangoObjectType): class Meta: model = models.Post interfaces = (graphene.Node, ) class Query(graphene.ObjectType): post = graphene.Field(PostType) posts = DjangoConnectionField(PostType) def resolve_posts(self, info, **kwargs): return models.Post.objects.order_by('-created_date', '-id') When I add a new post after fetching cursors and data, cursors change. In other words, the cursor that was pointing to the exact offset of data does not point that data any longer. It points a new, different data. Thereby, I cannot implement a cursor-based pagination by using: query fetchPosts ($cursor) { posts(first: 20, after: $cursor)... } Because cursor changes as data changes, it is no different than traditional offset-based-pagination. Is there something I am missing? What I want for the cursor is not to change. Like this article: https://www.sitepoint.com/paginating-real-time-data-cursor-based-pagination/ How should I make the cursor point the same data that changes dynamically? -
List all the permissions and Select/ Deselect/ Update for a specific user
I am working on a custom Admin app in Django project. I repeat, I DO NOT want to use the Django's inbuilt Admin interface/app provided. I am stuck at a point where I want to change the Permissions given to a specific User. I used the generic UpdateView for updation of some specific User's details like Name, Email, User Name etc. but when it came to updating the User Permissions, there was neither a button,link nor a checkbox. How can I ADD/Remove/Update the permissions of a User? Any help would be highly appreciated. class UserUpdate(UserPassesTestMixin, UpdateView): models = User fields = ('username', 'email', 'first_name', 'last_name', 'is_staff', 'is_active', 'is_superuser', 'user_permissions') def get_object(self, queryset=None): obj = User.objects.get(pk=self.kwargs['pk']) self.success_url = reverse_lazy('admin:user_detail', kwargs={'pk': obj.pk}) return obj def test_func(self): return self.request.user.is_superuser This is my view for updating the User Data by selecting a User. -
How many times does the Django ORM hit the database when calling a foreign key's primary key and using the in list filter?
Lets say I have the following models: class Foo(models.Model): ... class Bar(models.Model): name = models.CharField(max_length=255) foo = models.ForeignKey(Foo) Now, I am doing the following queries: foo_pks = set([]) for bar in Bar.objects.filter(name='some_name'): foo_pks.add(bar.foo.pk) for foo in Foo.objects.filter(pk__in=foo_pks): # Do something So basically, I am adding the primary keys to the set, and then using that set to make another query. How many times am I hitting the database? Moreover, is this horribly inefficient, and if so, is there a better way to do this?