Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django REST JSONParser - removes line breaks?
I have a small Django REST Framework app, located in my bigger Django app. It provides endpoints, which read back some data from the app's DB, and returns it in JSON for an integrated React Front End. Now, I have lately noticed, that one of the fields in a REST endpoint returns strings from DB, but strips them off any escaped characters (\r, \n etc.), once the string is pushed through Django REST's JSONParser. In my understanding, the information flow is as follows: user ---> urls.py ---> EndpointView ---> DjangoRESTSerializer -*-> JSONParser -**-> user When I was checking shape of data in the spot marked with one * above (Django REST's template for data, before we specify format we want it in), the string in question still had all the escape characters in it. However, after specifying on that web page that I wanted that data returned in JSON (spot with two *'s above), it would reload and return JSON-style view, where the escape characters were gone. Is that an intended Django REST Framework behavior? Is there any way I can prevent JSONParser from removing escape characters? Data presentation on the React app without them is just a wall of … -
How to create form field for multiple models in Django?
Tried to figure this out on my own but stumped - I'm working on a crm project to learn Django and have gotten stuck trying to incorporate activities between a user and client. Specifically, I'm trying to make it possible to record an email interaction and to have the from/to fields reference either a user or client model. So essentially an email can be recorded as either from a user to client or vice versa. The next part would be to allow for multiple clients or users to be tagged in the correct fields of this interaction. I've tried incorporating the to and from fields as models so that they can use the GenericForeignKey class like so: class Activity(models.Model): owner = models.ForeignKey(User, on_delete=models.DO_NOTHING) date = models.DateTimeField() class EmailTo(models.Model): content_type = models.ForeignKey(ContentType, on_delete=models.DO_NOTHING) object_id = models.PositiveIntegerField() content_object = GenericForeignKey('content_type') class EmailFrom(models.Model): content_type = models.ForeignKey(ContentType, on_delete=models.DO_NOTHING) object_id = models.PositiveIntegerField() content_object = GenericForeignKey('content_type') class EmailActivity(Activity): emailto = models.ForeignKey(EmailTo, on_delete=models.DO_NOTHING) emailfrom = models.ForeignKey(EmailFrom, on_delete=models.DO_NOTHING) body = models.TextField(blank=True) but now I'm stuck trying to figure out how to represent that on a form. I thought maybe I could use a union to combine two queries into one field using a ModelMultipleChoiceField: class EmailActivityForm(forms.ModelForm): emailto = … -
Django - Serializer not setting ManyToManyField
For some reason the following code isn't setting the hash_tags attribute under Post. The way I checked was I put a breakpoint at the return Response line in view.py and I checked the newly created Post object and the hash_tags attribute just returned an empty list. Also when I read the serializer.data, hash_tags is an empty list as well. Even though the HashTag table clearly created the hash tag found in the body. What's going on? model.py class Post(AbstractBaseModel): creator = models.ForeignKey( User, on_delete=models.CASCADE, related_name="post_creator") join_goal = models.ForeignKey(JoinGoal, on_delete=models.CASCADE) body = models.CharField(max_length=511, validators=[MinLengthValidator(5)]) hash_tags = models.ManyToManyField(HashTag) type = models.CharField( choices=PostType.choices, max_length=50, ) class HashTag(models.Model): hash_tag = models.CharField(max_length=140, primary_key=True, validators=[ MinLengthValidator(1)]) Serializer.py class HashTagSerializer(serializers.ModelSerializer): class Meta: model = HashTag fields = ['hash_tag'] class PostSerializer(serializers.ModelSerializer): hash_tags = HashTagSerializer(many=True, read_only=True) class Meta: model = Post fields = ('creator', 'join_goal', 'body', 'uuid', 'created', 'type', 'updated_at', 'hash_tags') view.py @api_view(['POST']) def post_create_update_post(request): user_uuid = str(request.user.uuid) request.data['creator'] = user_uuid request.data['type'] = PostType.UPDATE post_text = request.data['body'] hash_tags_list = extract_hashtags(post_text) hash_tags = [HashTag.objects.get_or_create(hash_tag=ht)[0].hash_tag for ht in hash_tags_list] request.data['hash_tags'] = hash_tags try: with transaction.atomic(): serializer = PostSerializer(data=request.data) if serializer.is_valid(raise_exception=True): post_obj = serializer.save() except Exception as e: return Response(dict(error=str(e), user_message=error_message_generic), status=status.HTTP_400_BAD_REQUEST) return Response(serializer.data, status=status.HTTP_201_CREATED) -
Django Channels - URL not found for only one endpoint
Have been using django channels for the first time on a new project, have it setup working great for 5 other apps. I have just come back to it to add a websocket for a new app, but I can't get django to recognize the url, I keep getting 'Not Found: /ws/globallayer/'. I've set it up exactly the same as I did for other apps, I don't know if I'm just forgetting a step to register the urls or something, hoping someone can see something I've missed. consumers.py import json from channels.generic.websocket import AsyncWebsocketConsumer class GloballayerConsumer(AsyncWebsocketConsumer): async def connect(self): self.group_name = 'globallayer' await self.channel_layer.group_add( self.group_name, self.channel_name ) await self.accept() async def disconnect(self, close_code): await self.channel_layer.group_discard( self.group_name, self.channel_name ) async def globallayer_update(self, event): data = event['data'] # Send message to WebSocket await self.send(text_data=json.dumps({ 'data': data })) routing.py from django.urls import re_path from . import consumers websocket_urlpatterns = [ re_path(r'^ws/globallayer/$', consumers.GloballayerConsumer.as_asgi()) ] project asgi.ppy file all_websocket_urlpatterns = [ # ... other websocket routes from different apps, *globallayer.routing.websocket_urlpatterns, ] application = ProtocolTypeRouter({ "http": get_asgi_application(), "websocket": AuthMiddlewareStack( URLRouter( all_websocket_urlpatterns ) ), }) -
Python/Django requests JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I am trying make Microservice warehouse / store. Everything is ok, but when I want to connect celery to my project, I am getting error Expecting value: line 1 column 1 (char 0) when trying to start celery @shared_task def shop_sync(): url = 'http://warehouse:8001/authors/' response_author = requests.get(url=url).json() while 1: for counter, data in enumerate(response_author['results']): Author.objects.get_or_create( id=data['id'], defaults={ 'id': data['id'], 'first_name': data['first_name'], 'last_name': data['last_name'] } ) if response_author['next']: response_author = requests.get(response_author['next']).json() else: break url = 'http://warehouse:8001/genres/' response_genre = requests.get(url).json() while 1: for counter, data in enumerate(response_genre['results']): Genre.objects.get_or_create( id=data['id'], defaults={ 'id': data['id'], 'name': data['name'] } ) if response_genre['next']: response_genre = requests.get( response_genre['next'] ).json() else: break url = 'http://warehouse:8001/books/' response = requests.get(url).json() while 1: for counter, data in enumerate(response['results']): book, created = Book.objects.get_or_create( id=data['id'], defaults={ 'id': data['id'], "title": data['title'], "description": data['description'], "image": data['image'], "language": data['language'], "status": data['status'], "price": data['price'], "isbn": data['isbn'], "pages": data['pages'], "created": data['created'], "available": data['available'], "quantity": data['quantity'], "genre": Genre.objects.get(id=data['genre']) } ) if not created: book.title = data['title'] book.description = data['description'] book.image = data['image'] book.language = data['language'] book.status = data['status'] book.price = data['price'] book.isbn = data['isbn'] book.pages = data['pages'] book.created = data['created'] book.available = data['available'] book.quantity = data['quantity'] book.genre = Genre.objects.get(id=data['genre']) book.save() for i in data['author']: author = Author.objects.get(id=i) book.author.add(author) if response['next']: … -
django models.FileField UnicodeEncodeError
I have this model. class MyModel(models.Model): ... video = models.FileField(upload_to='video/') ... def delete(self, *args, **kwargs): self.video.delete() super().delete(*args, **kwargs) An error happens when I try to delete the video. It writes. UnicodeEncodeError ... 'ascii' codec can't encode character '\u010d' in position 58: ordinal not in range(128) Exception Location: /usr/lib/python3.6/genericpath.py in isdir, line 42 How can I fix this? -
What is the best way to store big data per user?
I just need some advice about what database should I use, and how should I store my data. Namely I need to store big chunk of data per user, I was thinking about storing everything in JSON data, but I thought that I could ask you first. So I am using Django, and for now MySql, I need to store like 1000-2000 table rows per user, with columns like First Name, Last Name, Contact info, and also relate it somehow to the user that created that list. Also I need this to be able to efficiently get data from database. Is there any way of storing this big data per user? Thank you! -
Is it possible to pass the static file to be included as a context variable from the views.py?
in my django base template I have this: {% include vsm_x_win_template with vsm_x_win_body_contents='test_html' %} inside vsm_x_win_template.html is another include which I would like to be able to pass a variable to something like this. {% include {{ vsm_x_win_body_contents }} %} <--I need to dynamically set the value I'm new to Django but it seems something like this might be possible? -
Django - 'Invalid pk - object does not exist' for ManyToMany relation
For some reason at if serializer.is_valid(raise_exception=True) my code is complaining about the list hash_tags, which should be primary keys of the class HashTag that the keys in the list aren't valid, but as you can see I have a line under views.py, which is: hash_tags = [HashTag.objects.get_or_create(hash_tag=ht)[0].hash_tag for ht in hash_tags_list] that should be generate a valid list of primary keys. What is going on? serializer.py class Post(AbstractBaseModel): creator = models.ForeignKey( User, on_delete=models.CASCADE, related_name="post_creator") join_goal = models.ForeignKey(JoinGoal, on_delete=models.CASCADE) body = models.CharField(max_length=511, validators=[MinLengthValidator(5)]) hash_tags = models.ManyToManyField(HashTag) type = models.CharField( choices=PostType.choices, max_length=50, ) class HashTag(models.Model): hash_tag = models.CharField(max_length=140, primary_key=True, validators=[ MinLengthValidator(1)]) # No update added, because cannot be edited. Can only be added and deleted Serializer.py class PostSerializer(serializers.ModelSerializer): class Meta: model = Post fields = ('creator', 'join_goal', 'body', 'uuid', 'created', 'type', 'updated_at', 'hash_tags') view.py @api_view(['POST']) def post_create_update_post(request): """ POST endpoint for current user creating a goal update post """ user_uuid = str(request.user.uuid) request.data['creator'] = user_uuid request.data['type'] = PostType.UPDATE post_text = request.data['body'] hash_tags_list = extract_hashtags(post_text) hash_tags = [HashTag.objects.get_or_create(hash_tag=ht)[0].hash_tag for ht in hash_tags_list] request.data['hash_tags'] = hash_tags try: with transaction.atomic(): serializer = PostSerializer(data=request.data) if serializer.is_valid(raise_exception=True): post_obj = serializer.save() except Exception as e: return Response(dict(error=str(e), user_message=error_message_generic), status=status.HTTP_400_BAD_REQUEST) return Response(serializer.data, status=status.HTTP_201_CREATED) -
Django [UnicodeDecodeError: 'utf-8' codec can't decode byte 0x90 in position 595] when raise exception
In django when i raise exception [for example] if data: raise Exception('error') I always get [UnicodeDecodeError: 'utf-8' codec can't decode byte 0x90 in position 595] this error. i checked that all the files are encoded with utf-8 and there are no packages dependecies problem, there are no code that import file not english or open file. -
Connect Django and React
I have a react application built with dummy data that does stuff. This data is data that should be reached by making a request to my server. My React app is not connected to anything as of now I have to separate folders My folder with my react application My folder with my Django application My Django application is built - small application that has CRUD features. I want to integrate my React app with my Django application so that they work together. What would be the best way for doing this. -
Python Flask - Save data that can be accessed by multiple clients
I am trying to create an app that allows multiple clients to connect and use it as a video conferencing site(like google meets). This is how it would work(the different coloured clients represent different but simultaneous meetings): I am trying to find a way to store the clients video and audio data in a way that would allow other users to access it. I have thought about using file based system where a new file could be created for each meeting and that would store the relevant data but thought this may be quite slow. Another method I have thought about would be to use either a SQLite or MySQL database to store the data. I would appreciate any feedback on the methods I have mentioned above and/or any new method I haven't thought about. Thank you:) -
ValueError: Field 'maca' expected a number but got ''. Django error
I have a model like this class Product(models.Model): class Maca(models.IntegerChoices): Yes = 1, No = 2, maca = models.PositiveSmallIntegerField( db_column='maca', choices=Maca.choices ) and a form like this class ProductForm(forms.ModelForm): maca = forms.ChoiceField(required=False, choices=Product.Maca.choices, widget=forms.RadioSelect()) I have one case that this field will not show on the form. But when it is not in the form, the field 'maca' will submitted as '' (Blankn) and I'm getting an error for this ValueError: Field 'maca' expected a number but got ''. Can someone help me please? How can I send this in database as None value? -
How to use database in Heroku
I am new in web applications and I created first application in django. I decided to deployment my project in Heroku. But I'm so confused about database. I click "Heroku pricing" and I see 4-5 options. Free, Hobby, Standart, etc. And then i see databases options except these options. What is this?? as I said, this is my first applications and I dont know much about web servers. but as far as I know, service providers offer certain packages. These packages include RAM, Storage, Traffic, Database, ... etc. In short, my questions are: Is database included in heroku pricing? I saw "connection 0 of 20" while using the heroku database. Does this mean only 20 users can access the site? When I browsed the Heroku site, I didn't see any storage information in the pricing section. no storage pricing on cloud-based deployments? Can I create a different database outside of the heroku environment and connect it to heroku? every answer given informs and improving me. Thank you -
Django - drf-yasg setting request_body in @swagger_auto_schema to reduced version of serializer on @api_view
Is there a way to set request_body for @swagger_auto_schema to only be part of a Serializer? The reasoning being as you can see below the creator is set by the current user object passed by authenticator to the post_create view, but if I set request_body to the PostSerializer that'll be confusing for others, because they'll assume it needs a creator attribute, even though that's parsed from the user. Is there a way I can set request_body for this endpoint that uses @api_view with some of PostSerializer? view.py @api_view(['POST']) @swagger_auto_schema( operation_description="Create a post object" ) def post_create(request): try: request.data['creator'] = str(request.user.uuid) post_serializer = PostSerializer(data=request.data) if post_serializer.is_valid(raise_exception=True): post_obj = post_serializer.save() except ValidationError as e: return Response(dict(error=str(e), user_message=error_message_generic), status=status.HTTP_400_BAD_REQUEST) return Response(post_serializer.data, status=status.HTTP_201_CREATED) serializer.py class PostSerializer(serializers.ModelSerializer): class Meta: model = Post fields = ('creator', 'body', 'uuid', 'created', 'updated_at') -
Как разделить сервер авторизации и остальную работу с данными на два разных сервера
Я начинаю писать сайт на django-rest-framework и пишу авторизацию с использованием jwt токенов.И я прочитал что сервер авторизации должен быть отделён от остальной логики, но я вообще не могу найти как это сделать. Я видел пример что нужно использовать django aouth toolkit но не могу найти не одного примера работы с ним. По возможности прошу очень подробное объяснение или ссылки на источники. Заранее спасибо) -
How to implement CI/CD pipeline for Django application
I have a Django project and I want to implement CI/CD pipeline for the project but I really don't know how to go about it. Can someone guide me or refer a comprehensive article on that. Am using gitlab -
Why does my django template not recognize the url parameters I set in my views.py file?
I am building an event management app using Django. I have created a dynamic calendar, and I'm trying to add a link to next months calendar in my navigation. In my views.py file I added parameters like so: def home(request, year=datetime.now().year, month=datetime.now().strftime('%B')): <li class="nav-item"> <a class="nav-link" href="{% url 'home' '2021' 'November' %}">November</a> </li> When I add the two arguments, I get a template rendering error: Reverse for 'home' with arguments '('2021', 'November')' not found. 1 pattern(s) tried: ['$'] When I remove the two arguments and only leave 'home' the template error goes away, and I'm not sure why. Any help is appreciated! -
default value dont work in django inline formset
I'm trying to set default value for my django inline formset , but it only shows for the first form , the others are empty ! class Booking(models.Model): admin = models.ForeignKey(User,on_delete=models.CASCADE) takes_by = models.ManyToManyField('vistors.Vistor',through='BookingVisitor',related_name='vistors') class BookingVisitor(models.Model): visitor = models.ForeignKey('vistors.Vistor',on_delete=models.PROTECT,related_name='visitor_booking') booking = models.ForeignKey(Booking,on_delete=models.PROTECT,related_name='booking_bookingvisitors') reason = models.CharField(max_length=50,default='some text') but its doesnt work !? i also tried this in my view initial_data = {'reason':'some text'} VistorsInlineFormset(prefix='formset',initial=initial_data) but it raise this error : KeyError at /en/some/url 0 and this is my forms.py class BookingVisitorForm(forms.ModelForm): visitor = forms.ModelChoiceField( queryset=Vistor.objects.all().order_by('-pk'),empty_label='--------', ) class Meta: model = BookingVisitor fields = ['visitor','reason'] VistorsInlineFormset = inlineformset_factory(Booking,BookingVisitor,form=BookingVisitorForm,extra=1,can_delete=True) -
Django - Unable to get values from input field
I'm currently working on a Django project where I am trying to retrieve values from HTML input fields when a submit button is clicked. One is plane text and two are datetime fields. The view that is called runs when the button is clicked (I've hard coded values within my view function to test) but when I print the values of the input fields I'm trying to retrieve I get None for all three. The form I'm using in my html template looks like so: <form action="{% url 'get-billing'%}" method='GET'> {% csrf_token %} <button type="submit" value="click" class="btn btn-warning btn-sm fs-6">Submit</button> </form> views.py: def get_billing(request): url='placeholder for api url' secret='placeholder for api key' client = request.GET.get('client-name') start = request.GET.get('start-date') end = request.GET.get('end-date') print(client) print(start) print(end) response = requests.get(url, headers={ 'key': secret, 'start': '2021/10/01', 'end': '2021/10/02', }).json() # print(request) context = {'data':response} return render(request, 'billing/billing.html', context) urls.py: path('get_billing',views.get_billing, name='get-billing'), And this is what the 3 print statements return even though there are values in all 3. None None None Anyone know what I am doing wrong here? -
Django. Using a variable as a part of a variable name
I have a block of code that needs to be repeated over a hundred times with different input variables. The first 3 letters can change and the numbers can change. For example: BTCH1_13 maybe become BTCH1_18 or MESH1_13 I would need to use (var x)H1_(var y) and somehow be able to input them. I'm pretty sure there's a way to do this. Is there a term for this and how do I do it? </td> <td class="13"> {% if BTCH1_13.isBreak == '!' %}!{% endif %} {% if BTCH1_14.isPUpFractal == 'pUP' %}pUP{% endif %} {% if BTCH1_14.isPDnFractal == 'pDN' %}pDN{% endif %} {% if BTCH1_15.isUpFractal == 'UP' %}UP{% endif %} {% if BTCH1_15.isDnFractal == 'DN' %}DN{% endif %} {% if BTCH1_14.isPSG == 'PSG' %}PSG{% endif %} {% if BTCH1_15.isSG == 'SG' %}SG{% endif %} <br> {{BTCH1_13.maxRetracement|default_if_none:""}}|{{BTCH1_13.Current_RP|default_if_none:""}}|{{BTCH1_13.maxProjection|default_if_none:""}} </td> -
Django - how to use UpdateView modelform with custom field?
I would like to use a custom form field with an UpdateView. My code so far is: models.py class CustomUser(AbstractUser): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) paper = models.BooleanField(default=False) pronouns = models.CharField(max_length=50, blank=True) def __str__(self): return self.username forms.py class CustomUserProfileForm(forms.ModelForm): pronouns = forms.CharField(label='Pronouns', required=True) class Meta: model = get_user_model() fields = ('first_name', 'last_name') def __init__(self, *args, **kwargs): _pronoun_list = ('he/him/his', 'she/her/hers', 'they/them/theirs') super(CustomUserProfileForm, self).__init__(*args, **kwargs) self.fields['pronouns'].widget = ListTextWidget( data_list=_pronoun_list, name='pronoun-list') This form references ListTextWidget although I don't think it directly relates to my issue: class ListTextWidget(forms.TextInput): def __init__(self, data_list, name, *args, **kwargs): super(ListTextWidget, self).__init__(*args, **kwargs) self._name = name self._list = data_list self.attrs.update({'list': 'list__%s' % self._name}) def render(self, name, value, attrs=None, renderer=None): text_html = super(ListTextWidget, self).render( name, value, attrs=attrs) data_list = '<datalist id="list__%s">' % self._name for item in self._list: data_list += '<option value="%s">' % item data_list += '</datalist>' return (text_html + data_list) Views.py class CustomUserProfileview(UpdateView): model = CustomUser form_class = CustomUserProfileForm template_name = 'account/customuser_change_form.html' success_url = reverse_lazy('home') def form_valid(self, form): user = self.request.user pronouns = form.cleaned_data['pronouns'] user.pronouns = pronouns user.save() return super().form_valid(form) The form is displayed as I would expect, where I can change the first name, last name and choose pronouns from a list. If I print(user) and print(pronouns) I get … -
What's the difference between _set() and filter() using a Foreign Key in django?
Let's say I have a User and Order model. Order has a fk User. What's the difference between doing def getorders(request): user = request.user orders = user.order_set.all() and def getorders(request): user = request.user orders = Order.objects.filter(user=user) Both will return the same result, no ? So what's the benefits to use _set instead of filter ? -
How to get all related objects of all objects in a Queryset?
Unfortunately I cant find a straight-forward answer to this even though there are several related quesitions. Say we have: class Category(models.Model): name = models.CharField(max_length=50) class SubCategory(models.Model): name = models.CharField(max_length=50) category = models.ForeignKey(Category,on_delete=CASCADE, related_name='subcategories') I know I can get all the subcategories of a specific category by some_category.subcategories.all() But how do I get a queryset of all subcategories of all categories in a queryset? -
Django forms field
class Registration(forms.Form): Name=CharField() So, CharField input is showing in the browser but when I submit this field without filling any data it's doesn't show django error. And if I apply min_length and max_length both are not working What should I do