Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
static fills in django
I get this error when I run collectstatic File "C:\django\venv\lib\site-packages\django\contrib\staticfiles\storage.py", line 38, in path raise ImproperlyConfigured("You're using the staticfiles app " django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path. enter image description here I learned the code from an educational video Does anyone know the reason ??? Thank you for answering -
What are the best practices for storing duplicated data in Django models such as first name, last name, and full name
I have built a project with a React frontend and a Django Rest Framework (DRF) API for my backend. Things are going well but I am quite curious about best practices for data saved against your models. As an example, I have a user model with first_name and last_name fields. There is a one-to-one relationship between my user and the two user domains (let's say Buyer and Seller as an example). In addition, we have a foreign key relationship for Seller on something like a Product. On my UI, I have tables that display the data and in general, the majority of tables display the user's full name (first_name + last_name). Generally, all table fields I will want to filter by and order by. I decided that I wanted the data returned from the REST API to represent the table data so I am now returning full_name by augmenting the serializer of Buyer and Seller to have full name using a SerializerMethodField like so: full_name = serializers.SerializerMethodField() ... def get_full_name(self, obj) -> str: return obj.user.get_full_name() However, I will also need to do this in all places where I want to show the Buyer/Seller where they are referenced by a Foreign … -
Django TestCase client.logout() is not logging out user correctly
I'm writing some tests for Django. I'm having trouble with the client.logout() method, which doesn't seem to be logging the test client out. Here's my setUp(): def setUp(self): # Set up a user to use with all of the tests and log in the user. User.objects.create_user(username='temporary', password='temporary') self.client.login(username='temporary', password='temporary') # Create second user for later use. User.objects.create_user(username='temporary2', password='temporary2') And here's my test method: def test_user_can_only_access_own_recipes(self): """ A user should only be able to access recipes they have created. """ # temporary1 user already logged in via setUp(). user_1_recipe = self.create_recipe() self.client.logout() # Login second user and create recipe. self.client.login(username='temporary2', password='temporary2') user_2_recipe = self.create_recipe() # Response_owned is a recipe made by user 2, who is still logged in, so the recipe should be viewable. # Response_not_owned is recipe made by user 1 and should not be viewable as they are logged out. response_owned = self.client.get( reverse('recipes:display_recipe', args=(user_2_recipe.id,))) response_not_owned = self.client.get( reverse('recipes:display_recipe', args=(user_1_recipe.id,))) self.assertEqual(response_not_owned.status_code, 403) # Convert to str, otherwise Django compares 'temporary' with just temporary no quotes. self.assertEqual(str(user_1_recipe.user), 'temporary') self.assertEqual(user_2_recipe.user, 'temporary2') self.assertEqual(response_owned.status_code, 200) This fails with the assertion error: self.assertEqual(user_2_recipe.user, 'temporary2') AssertionError: <User: temporary> != 'temporary2' So my test should create a recipe owned by user 1. User 1 should be … -
I need to limit my update and my delete to Purchases with status "EV"
I need to limit my update and my delete to Purchases with status "EV", those with status "AP", can only be read. Here is my models: class Purchases(TimeStampedModel): values = models.DecimalField(decimal_places=2, max_digits=10, default=0) cpf = BRCPFField("CPF") status = models.CharField(max_length=2, choices=[('AP', ('Aprovado')), ('EV', ('Em validação'))]) modified = datetime.today().strftime('%d-%m-%y %H:%M') def save(self, *args, **kwargs): if self.cpf == "15350946056": self.status = "AP" super(Purchases, self).save(*args, **kwargs) else: self.status = "VA" super(Purchases, self).save(*args, **kwargs) @property def clashback(self): api = requests.get("https://mdaqk8ek5j.execute-api.us-east-1.amazonaws.com/v1/cashback?cpf=12312312323") date = json.loads(api.content) valor_carteira = date['body']['credit'] if self.values <= 1000: info_payments = [("porcentual", "10%"), ("valor_clashback", self.values * Decimal('0.1')), ("valor_total", {self.values * Decimal('0.1') + valor_carteira})] return info_payments elif self.values <= 1500: info_payments = [("porcentual", "15%"), ("valor_clashback", self.values * Decimal('0.15')), ("valor_total", {self.values * Decimal('0.15') + valor_carteira})] return info_payments else: info_payments = [("porcentual", "20%"), ("valor_clashback", self.values * Decimal('0.20')), ("valor_total", {self.values * Decimal('0.20') + valor_carteira})] return info_payments def __str__(self): return self.values, self.cpf, self.clashback Here is my viewsets: from rest_framework.permissions import SAFE_METHODS from rest_framework.viewsets import ModelViewSet from orders.api.serializers import PurchasesInfoSerializers, PurchasesSerializers from orders.models import Purchases class PruchasesViewSets(ModelViewSet): queryset = Purchases.objects.all() filter_fileds = ("cpf",) def get_serializer_class(self): if self.request.method in SAFE_METHODS: return PurchasesInfoSerializers return PurchasesSerializers def get_queryset(self): def get_queryset(self): return super().get_queryset().filter(status='AP') I tried to create a filter to show only with status … -
Django template - Calculate the number of months between today and a variable date
I have a variable that is a date and I want to calculate the months' difference between it and today. The first try was to calculate this in the view but I prefer to do this calculation on the UI when it is needed. I have tried declaring and subtracting the values in the sample code here but I ran into syntax errors though in python this works well. This is what I tried in the templates {% with duration=(organisation.date_founded.year - today.year) * 12 + (date_started.month - today.month) %} { duration }} % endwith %} This is the python version of it today = datetime.datetime.today() num_months = (date_started.year - today.year) * 12 + (date_started.month - today.month) -
Django query which calculate most active post by like and dislike, and by each category
I want to calculate most popular post by each category, but i have this error DISTINCT ON fields is not supported by this database backend. after i use PostgreSql, but I also had a error. annotation and distinct together did not work model --> class Category(models.Model): title = models.CharField(max_length=150, verbose_name=_("კატეგორია")) def __str__(self): return self.title class Question(models.Model): user = models.ForeignKey( User, on_delete=models.CASCADE, verbose_name=_("მომხმარებელი") ) category = models.ManyToManyField(Category) title = models.CharField(max_length=150, verbose_name=_("სათაური")) body = models.TextField(verbose_name=_("ტექსტი")) image = models.ImageField(blank=True, null=True, verbose_name=_("ფოტო")) link = models.URLField( max_length=400, blank=True, null=True, validators=[RequireHttpOrHttpsUrl()], verbose_name=_("ლინკი"), ) time = models.DateTimeField(auto_now=True) send_notification = models.BooleanField( default=True, verbose_name=_("გავაგზავნოთ შეტყობინება?") ) def __str__(self): return self.title class LikeDislike(models.Model): user = models.ForeignKey( User, on_delete=models.CASCADE, verbose_name=_("მომხმარებელი") ) question = models.ForeignKey( Question, on_delete=models.CASCADE, verbose_name=_("კითხვა") ) point = models.BooleanField() time = models.DateTimeField() def __str__(self): return self.question.title view -> class CountLikeByCategory(generics.ListCreateAPIView): serializer_class = CountLikeByCategorySerializer def get_queryset(self): query=Question.objects.values_list( 'category__title','title' ).annotate( l=Count('likedislike',filter=Q(likedislike__point=1)), d=Count('likedislike',filter=Q(likedislike__point=0)), total=F('l')+F('d'), ).order_by('category', '-total').distinct('category') return query -
I'm working on an ecommerce project on mysql and django ,and during add to cart function i can't figure out where the trouble is ...?
i can't figure out where the trouble is ...? THis is the error occured * Environment: Request Method: GET Request URL: http://127.0.0.1:8000/cart/ Django Version: 3.2.7 Python Version: 3.8.10 Installed Applications: ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'myapp.apps.MyappConfig', 'crispy_forms'] Installed Middleware: ['django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware'] Traceback (most recent call last): File "/home/akshay/Django/my_env/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner response = get_response(request) File "/home/akshay/Django/my_env/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/home/akshay/Django/my_env/lib/python3.8/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view return view_func(request, *args, **kwargs) File "/home/akshay/Django/Frooty/myapp/views.py", line 61, in add_cart customer = request.user.customer File "/home/akshay/Django/my_env/lib/python3.8/site-packages/django/utils/functional.py", line 247, in inner return func(self._wrapped, *args) File "/home/akshay/Django/my_env/lib/python3.8/site-packages/django/db/models/fields/related_descriptors.py", line 421, in __get__ raise self.RelatedObjectDoesNotExist( Exception Type: RelatedObjectDoesNotExist at /cart/ Exception Value: User has no customer. THis is my models.py from django.db import models from django.contrib.auth.models import AbstractUser,User class Customer(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, blank=True) name = models.CharField(max_length=200, null=True) email = models.CharField(max_length=200, null=True) def __str__(self): return self.name THis is my views.py @login_required def add_cart(request): if request.user.is_authenticated : customer = request.user.customer order,created = Order.objects.get_or_create(customer=customer, complete=False) items = order.orderitem_set.all() else: item = [] context = {'items':items} return render(request,'store/cart.html', context) This is my forms.py from django import forms from django.contrib.auth.forms import UserCreationForm from django.contrib.auth.models import User # Create your … -
Connect django to legacy postgresql database, already exist and have 1 table
am trying to connect postgresql database with a table already created (legacy). Not much documentation on the Internet to do so as the database already created. Am afraid to lose the data. Any guide to do that. Am using Windows and gitbash and have pgadmin4. Still learning. Default database in settings file changed to my database but what's next migrate or inspectdb or create tables in django?! Any help is appreciated. -
What is the equivalent for, SELECT * FROM table_name WHERE id IN (1,2,5,9) in Django
def post(self, request, *args, **kwargs): req = self.request.POST ids = req.get('ids') imported_data =table_import.objects.filter(id=ids) in which ids has values 1,2,5,7 -
Find objects with many-to-many relations Django
I have a model class Book(models.Model): name = models.CharField() class BookShelf(models.Model): books = models.ManyToManyField(Book, on_delete=models.CASCADE) I need to find a bookshelf with both book1 and book2 objects This works, but I don't think it is optimal shelf = BookShelf.objects.filter(books=book1).filter(books=book2) This should work too, but it doesn't: from django.db.models import Q shelf = BookShelf.objects.filter(Q(books=book1) & Q(books=book2)) What did i do wrong? -
I want to show img in my template from database?
i want to show image from model.py file in user app where i uplode my image. this is user.model.py file model.py from django.db import models from django.contrib.auth.models import User from PIL import Image class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) image = models.ImageField(default='default.jpg', upload_to = 'profile_pics') def __str__(self): return f'{self.user.username} profile.' def save(self): super().save() img = Image.open(self.image.path) if img.height>300 or img.width > 300: img.thumbnail( (300,300) ) img.save(self.image.path) blog.model.py(blog is another app) . this is blog.model.py file model.py from django.db import models from django.utils import timezone from django.contrib.auth.models import User from django.urls import reverse class Post(models.Model): title = models.CharField(max_length=100,help_text="enter within 100 charecter") content = models.TextField() date_posted = models.DateTimeField(default=timezone.now) author = models.ForeignKey(User, on_delete=models.CASCADE) def __str__(self): return self.title # this is for show demo form admin site (if you use this than genarate a button for demo ) def get_absolute_url(self): return reverse('post-detail', kwargs={'pk' : self.pk}) And this is my class base view funcatin in blog app Whose template name is home.html. this is blog.view.py file view.py class PostListView(ListView): model = Post template_name = 'blog/home.html' context_object_name = 'posts' ordering = ['-date_posted'] paginate_by = 5 def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['ip'] = self.request.session.get('ip', 0) return context home.html I show you only image tag. {% for … -
Why is my Django object deleted with no reason?
I know this question may not be in appropriate shape, but I have no other way to describe it. I have a Django project, deployed with Heroku. I'm using Postgres add-on. The problem is, few of the objects that my users created is being deleted for some reason. I tried to see if there's a pattern with this issue, but I haven't found one. It happens randomly. (This usually happens when I share(copy+paste) a user's post to other people, but I'm not sure if it's a pattern.) I know it's not the heroku ephemeral filesystem issue, because I'm using the Postgres add-on. So I started wondering if there could be something wrong with Heroku or the Postgres add-on itself. This is a quite serious issue, but I can't figure out what the problem is. Has anyone experienced similar issue or knows the answer to this? Thank you very much. -
Django Unique Constraint Failures
I am trying to create a meal planner with react and django. I am having issues with the form and getting it to the backend. Right now I can submit the form from react and I get the data back to django correctly. Unless I need to add another mealItem for the same date and serviceType ('lunch', 'dinner'). I get the unique constraint error for the following fields: "meals_meal.menu_id, meals_meal.date, meals_meal.type, meals_meal.url". My options are to either re do the form on the frontend or change the way I deal with the data on the back end. For the front end I have a button that adds a form. Depending on the button clicked, the date, serviceType is selected to send to the backend. The form has an text input for item name, a type ('entre','side','other'), and diet type ('vegetarian', gluten_free, diary_free) Any help figuring this out would be awesome thank you. I should add most of the model was set up by my brother in law, but due to knew family member. He doesn't have much time to help me. Models.py class Menu(models.Model): name = models.CharField(max_length=100) users = models.ManyToManyField(User, related_name='menus') created_at = models.DateTimeField( auto_now_add=True, editable=False, ) updated_at = models.DateTimeField( … -
Getting postgres connected in docker-compose
So I have a successfully built image where I have postgres mounted as a volume, however I keep getting django.db.utils.OperationalError: could not connect to server: Connection refused- when I ran docker-compose up. My Dockerfile and docker-compose.yml looks like this: # syntax=docker/dockerfile:1 FROM python:3.10-slim-buster ENV PYTHONDONTWRITEBYTECODE 1 ENV PYTHONUNBUFFERED 1 WORKDIR /track COPY requirements.txt . RUN apt-get update \ && apt-get -y install libpq-dev gcc \ && pip install --upgrade pip \ && pip install --default-timeout=100 -r requirements.txt COPY . . ENV DJANGO_SETTINGS_MODULE track.settings EXPOSE 8000 version: "3.9" services: django: build: . command: python manage.py runserver 0.0.0.0:8000 stdin_open: true tty: true volumes: - .:/track ports: - "8000:8000" depends_on: - db db: image: postgres:9.6.10-alpine restart: always user: postgres volumes: - track_db:/var/lib/postgresql/data environment: POSTGRES_USER: postgres POSTGRES_PASS: postgres POSTGRES_DB: postgres volumes: track_db: {} Please, what am I doing wrong? -
userserializer asking for groups and user permissions
I am trying to create employees under a company like the following but the Userserializer is throwing errors for groups and user_permissions Models.py class Employee(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True) company = models.ForeignKey(Company, on_delete=models.CASCADE) name = models.CharField(max_length=500, default=0) email = models.EmailField(max_length=500, default=0) phone = models.CharField(max_length=500, default=0) city = models.CharField(max_length=500, default=0) role = models.CharField(max_length=100, default='View') #View/CRUD created = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) groups = models.ManyToManyField(Group, blank=True) Views.py class EmployeeCreateAPIView(CreateAPIView): queryset = User.objects.all() serializer_class = UserSerializer permission_classes = (permissions.AllowAny,) def post(self, request, format=None, **kwargs): data = {} for key in request.data: data[key] = request.data[key] print("data", data) serializer = UserSerializer(data=data) print("pre serializer ", serializer) ##### this if serializer.is_valid(): print("s data", serializer.data) Employee.objects.create(user_id=serializer.data["id"], company=Company.objects.get(pk=data['company']), name = serializer.data['first_name'] + ' ' + serializer.data['last_name'], email=serializer.data['email'], ) serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) Where pre seriliazer print statement shows the following: UserSerializer(data={'first_name': 'Ejaz', 'last_name': 'Khan', 'username': 'ash', 'email': 'ekhan5347@gmail.com', 'password': '9', 'confirm': '9', 'company': 5}): id = IntegerField(label='ID', read_only=True) password = CharField(write_only=True) last_login = DateTimeField(allow_null=True, required=False) is_superuser = BooleanField(help_text='Designates that this user has all permissions without explicitly assigning them.', label='Superuser status', required=False) username = CharField(help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, validators=[<django.contrib.auth.validators.UnicodeUsernameValidator object>, <Unique Validator(queryset=User.objects.all())>]) first_name = CharField(allow_blank=True, max_length=30, required=False) last_name = … -
How can I resolve i18n patterns error in settings.urls?
I'm trying to add language support to my app using django-model translation. But I am getting the following error in working urls located in root directory. how can i solve this from django.contrib import admin from django.urls import path, include from django.utils.translation import gettext_lazy as _ from django.conf.urls.i18n import i18n_patterns from New import views as new_views from Home import views as home_views urlpatterns = [ path('admin/', admin.site.urls), path("i18n/", include("django.conf.urls.i18n")), ] home_patterns =([path('', home_views.Index, name="index"), path(_('sistem-cozumumuz/'), home_views.Solution, name='solution'), path(_('teklif-isteyin/'), home_views.OfferRequests, name="offer-request"), path(_('gizlilik-politikasi/'), home_views.PrivacyPolicy, name='policy'), path(_('iletisim/'), home_views.Contact, name='contact'), ]) news_patterns =([path('', new_views.Index, name="index"), path(_('referanslar/'), new_views.References, name="reference"), path(_('yorumlar/'), new_views.Comments, name="event"), path(_('basinda-biz/'),new_views.News,name="new"), path(_('dokumanlar/'), new_views.Downloads, name="download"), path(_('<slug:slug>/'),new_views.NewDetails,name="new-detail"), path(_('yorumlar/<slug:slug>/'),new_views.CommentDetails,name="comment-detail"), path(_('referanslar/<slug:slug>/'),new_views.ReferenceDetails,name="reference-detail"), ]) urlpatterns += i18n_patterns( path('', include(home_patterns, namespace= 'Home'), path(_('haberler/'), include(news_patterns, namespace ='New')), )) Error Message: *** File "/home/ubuntu/Desktop/Makine_Sitesi/XMachine/urls.py", line 48, in path('', include(home_patterns, namespace= 'Home'), File "/home/ubuntu/.local/lib/python3.8/site-packages/django/urls/conf.py", line 38, in include raise ImproperlyConfigured( django.core.exceptions.ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported. Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead.*** -
Show appointments not included in range
I want to show dates that appointments are not booked in a template. For this reason i have so far: # Collect only the dates so that i can find what is not in that range. example_dates = Appointment.objects.values_list('start_appointment', flat=True) # Initialize start and end date start_date = datetime.date.today() end_date = start_date + datetime.timedelta(days=5) # Initialize new list that will include records does not exists not_found_dates = [] # Loop through in date range and if date does not exists # Create a dict and add it to the list for n in range(int((end_date - start_date).days)): new_date = start_date + datetime.timedelta(days=n) if new_date not in not_found_dates: not_found_dates.append(new_date) print(new_date) # Get original queryset examples = Appointment.objects.filter(start_appointment__range=(start_date, end_date)).values('start_appointment') print(examples) # Convert it to a list examples = list(examples) return render(request, 'appointments/add-appointment.html', {'examples': examples, 'not_found_dates': not_found_dates}) When i print the new_date from the loop i got: 2021-11-22 2021-11-23 2021-11-24 2021-11-25 2021-11-26 And the query from examples returns that i have 3 appointments in db in that range (2)on 2021-11-23 and (1) on 2021-11-22. Is it possible to show the dates that appointments not booked i.e 2021-11-24, 2021-11-25, 2021-11-26. -
Unable to install Pillow for Django App using Terminal on Namecheap Shared hosting
I was going to deploy my Django App on Namecheap shared hosting. My app needs Pillow to be able to run perfectly. But while installing pillow using pip install Pillow in Namecheap's Terminal, I get the error. I installed Django and other libraries successfully. But while installing Pillow, it gives me this error. Collecting Pillow==8.4.0 Using cached Pillow-8.4.0.tar.gz (49.4 MB) Preparing metadata (setup.py) ... done Building wheels for collected packages: Pillow Building wheel for Pillow (setup.py) ... error ERROR: Command errored out with exit status 1: command: /home/abduxdcv/virtualenv/iffi-store-app/3.8/bin/python3.8_bin -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-u2_5lsia/pillow_aabaeed7df664fd985a82d84f11f5eac/setup.py'"'"'; __file__='"'"'/tmp/pip-install-u2_5lsia/pillow_aabaeed7df664fd985a82d84f11f5eac/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-vyxwq5up cwd: /tmp/pip-install-u2_5lsia/pillow_aabaeed7df664fd985a82d84f11f5eac/ Complete output (143 lines): /opt/alt/python38/lib64/python3.8/distutils/dist.py:274: UserWarning: Unknown distribution option: 'long_description_content_type' warnings.warn(msg) /opt/alt/python38/lib64/python3.8/distutils/dist.py:274: UserWarning: Unknown distribution option: 'project_urls' warnings.warn(msg) running bdist_wheel running build running build_py creating build creating build/lib.linux-x86_64-3.8 creating build/lib.linux-x86_64-3.8/PIL copying src/PIL/ImtImagePlugin.py -> build/lib.linux-x86_64-3.8/PIL copying src/PIL/BmpImagePlugin.py -> build/lib.linux-x86_64-3.8/PIL running egg_info writing src/Pillow.egg-info/PKG-INFO writing dependency_links to src/Pillow.egg-info/dependency_links.txt writing top-level names to src/Pillow.egg-info/top_level.txt reading manifest file 'src/Pillow.egg-info/SOURCES.txt' reading manifest template 'MANIFEST.in' warning: no files found matching '*.c' warning: no files found matching '*.h' warning: no files found matching '*.sh' warning: … -
Django-decouple and django-dotenv not pulling variables out of .env file
I apologize in advance if my question is too wide open but I am trying to hide my private passwords of my settings.py file. I created a .env file and and tried using django-dotenv and Django-decouple to access those variables. It works for a 'TEST' variable I write in there but for my 'EMAIL_PASS' and other variables which I have already set in there it pulls the old 'EMAIL_PASS' not the one set in the .env in my Django directory. I probably set the env variables incorrectly (as in not in a virtual environment) last time on a previous project but this time around I am in a local env. I tried doing os.environ.clear() and .pop('EMAIL_PASS') commands. This works while in the CLI but the data comes back somehow. For now I will just change 'EMAIL_PASS' to 'EMAIL_PASSWORD' but I have to figure out why I can't remove the old 'EMAIL_PASS'. I know this is kind of vague but I am hoping someone can see this and pinpoint the source of my confusion. Thanks very much, Darryl -
How to run a rabbitMQ task asynchronously in django rest framework
I have written two python scripts, separate from my django project, to put messages in a rabbitMQ queue, and to take from it. This is my producer script, that reads some sensor values from a file, and every second puts the value to the queue: import pika, csv, time url = 'secret_queue_url' params = pika.URLParameters(url) connection = pika.BlockingConnection(params) channel = connection.channel() channel.queue_declare(queue='hello') with open('sensor.csv', newline='') as csvfile: reader = csv.reader(csvfile, delimiter='\n') for row in reader: channel.basic_publish(exchange='', routing_key='hello', body=row[0]) print(row[0]) time.sleep(1) connection.close() And this is my consumer script: import pika from app.models import Measurement url = 'secret_queue_url' params = pika.URLParameters(url) connection = pika.BlockingConnection(params) channel = connection.channel() channel.queue_declare(queue='hello') def callback(ch, method, properties, body): measurement = Measurement.objects.create(sensor=3, energy_consumption=float(str(body))) measurement.save() channel.basic_consume('hello', callback, auto_ack=True) channel.start_consuming() connection.close() Measurement is a model inside my django project. Like I said, these two files are not included in my project structure, but I have modified my consumer script in order to insert a new measuremet every time a value is read from the queue. I want to integrate the consumer script into my django project, so that it will create a new measurement every time there is something on the queue, and I want this script to run asynchronously, … -
Django - long running tasks
Im looking for and advice for long running tasks in Django. This is my use case: I have an eshop order with over 200 products (rows). When I click on "process" Django runs a function (which consists of multiple smaller functions) to process data. This may take some time but while this task is running, I want to let a user know about the process status. During that time the order is "locked". My current theory is to use combination of Django Celery (or its equivalent) and Django channels. Once the task is done, Channels will push a message via websocket to the frontend and JS will change objects (button, status, texts...). When task starts, a function updates the status in DB. After task is finished, status in DB is updated again. Because the app is not SPA and im using standard Django views and templates, this solution would not work in case the task will end during page refresh. So after the page is loaded again, task is not running, WS wont send any update but at the same time DB query has old status. Example: When process starts I save status as "running". When Celery task is finished, … -
Periodic and Non periodic tasks with Django + Telegram + Celery
I am building a project based on Django and one of my intentions is to have a telegram bot which is receiving information from a Telegram group. I was able to implement the bot to send messages in Telegram, no issues. In this moment I have a couple of Celery tasks which are running with Beat and also the Django web, which are decopled. All good here. I have seen that the python-telegram-bot is running a function in one of the examples (https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/echobot.py) which is waiting idle to receive data from Telegram. Now, all my tasks in Celery are in this moment periodic and are called each 10 or 60 minutes by Beat. How can I run this non-periodic task with Celery in my configuration? I am saying non-periodic because I understood that it will wait for content until it is manually interrupted. Django~=3.2.6 celery~=5.1.2 CELERY_BEAT_SCHEDULE = { 'task_1': { 'task': 'apps.envc.tasks.Fetch1', 'schedule': 600.0, }, 'task_2': { 'task': 'apps.envc.tasks.Fetch2', 'schedule': crontab(minute='*/60'), }, 'task_3': { 'task': 'apps.envc.tasks.Analyze', 'schedule': 600, }, } In my tasks.py I have one of the tasks like this: @celery_app.task(name='apps.envc.tasks.TelegramBot') def TelegramBot(): status = start_bot() return status And as the start_bot implemenation, I simply copied the echobot.py example … -
Django NestedHyperlinkedModelSerializer not returning Foreign Key Field
Parent id or foreign key is not returning from child. I am trying drf-nested-routers example. models.py class Client(models.Model): name = models.CharField(max_length=255) class MailDrop(models.Model): title = models.CharField(max_length=255) client_id = models.ForeignKey(Client, on_delete=models.CASCADE) serializers.py class ClientSerializer(HyperlinkedModelSerializer): class Meta: model = Client fields = ['id', 'name'] class MailDropSerializer(NestedHyperlinkedModelSerializer): parent_lookup_kwargs = { 'client_pk': 'client_id', } class Meta: model = MailDrop fields = ['id', 'title'] views.py class ClientViewSet(viewsets.ViewSet): serializer_class = ClientSerializer def list(self, request): queryset = Client.objects.filter() serializer = ClientSerializer(queryset, many=True) return Response(serializer.data) def retrieve(self, request, pk=None): queryset = Client.objects.filter() client = get_object_or_404(queryset, pk=pk) serializer = ClientSerializer(client) return Response(serializer.data) class MailDropViewSet(viewsets.ViewSet): serializer_class = MailDropSerializer def list(self, request, client_pk=None): queryset = MailDrop.objects.filter(client_id=client_pk) serializer = MailDropSerializer(queryset, many=True) return Response(serializer.data) def retrieve(self, request, pk=None, client_pk=None): queryset = MailDrop.objects.filter(pk=pk, client_id=client_pk) maildrop = get_object_or_404(queryset, pk=pk) serializer = MailDropSerializer(maildrop) return Response(serializer.data) If I add fields = ['id', 'title', 'client_id'] in the MailDropSerializer, it throws the following error: AssertionError: `NestedHyperlinkedRelatedField` requires the request in the serializer context. Add `con text={'request': request}` when instantiating the serializer. If I add the request in the serializer context, the output is as follows: url: http://127.0.0.1:8000/clients/4/maildrops/ [ { "id": 3, "title": "Madison Mosley", "client_id": "http://127.0.0.1:8000/clients/4/" }, { "id": 4, "title": "Louis Chen", "client_id": "http://127.0.0.1:8000/clients/4/" } ] I added … -
Error migrating data with models from django-address app
I'm doing a data migration, and using django-address for geographical addresses. I am able to save an address, and everything else seems to work except when I try to assign an address to a building I get the error "Invalid address value." Migration file: from django.db import migrations def forward_data_migration(apps, schema_editor): Address = apps.get_model("address", "Address") Locality = apps.get_model("address", "Locality") Property = apps.get_model("myApp", "Property") db_alias = schema_editor.connection.alias Locality.objects.using(db_alias).bulk_create([ Locality(name="Foo",) ]) Address.objects.using(db_alias).bulk_create([ Address(raw="1 Fake street" locality=Locality.objects.get(name="Foo"), ), ]) Property.objects.using(db_alias).bulk_create([ Property(building_name="Test building", address=Address.objects.get(raw="1 Fake street") ), ]) Property model: class Property(UUIDTimeStampedModel): building_name = models.CharField(max_length=255) address = AddressField(null=True, blank=True) Address models: https://github.com/furious-luke/django-address/blob/master/address/models.py The Address app seems to have a function that checks whether the value being passed to it is a valid instance of "Address" but I don't know why this check fails. -
how to make ImageFiel json serializable - django
I'm trying to add image to my to posts , but when i try to add a new post it raise this error : TypeError: Object of type ImageFieldFile is not JSON serializable and here is my models.py class ModelCategory(models.Model): admin = models.ForeignKey(User,on_delete=models.PROTECT) category = models.ForeignKey(Category,on_delete=models.PROTECT) model = models.CharField(max_length=40) giga = models.ForeignKey(Giga,on_delete=models.PROTECT) image = models.ImageField(upload_to='images',blank=True,null=True) and here is my views.py and forms.py @login_required def create_modelcategory(request): form = ModelCategoryForm() if request.is_ajax() and request.method == 'POST': form = ModelCategoryForm(request.POST,request.FILES) if form.is_valid(): obj = form.save(commit=False) obj.admin = request.user obj.save() return JsonResponse({'success':True},status=200) else: return JsonResponse({'sucess':False,'error_msg':form.errors},status=400) context = { 'form':form } return render(request,'storage/modelcategory.html',context) class ModelCategoryForm(forms.ModelForm): category = forms.ModelChoiceField(queryset=Category.objects.all().order_by('-pk')) giga = forms.ModelChoiceField(queryset=Giga.objects.all().order_by('-pk')) class Meta: model = ModelCategory fields = ['category','model','giga','image'] i tried this solution but still i'm not sure how to use it json.dumps(str(my_imagefield)) $(document).ready(function () { const image = document.getElementById('id_image') const imgBox = document.getElementById('img-box') const category = document.getElementById('id_category') const model = document.getElementById('id_model') const giga = document.getElementById('id_giga') const min_qnt = document.getElementById('id_min_qnt') const csrf = document.getElementsByName('csrfmiddlewaretoken') image.addEventListener('change', ()=>{ const img_data = image.files[0] const url = URL.createObjectURL(img_data) console.log(url) imgBox.innerHTML = `<img src="${url}" width="100%">` }) const create_modelcategory_form = document.getElementById('create-modelcategory') create_modelcategory_form.addEventListener("submit",submitModelCategoryHandler); function submitModelCategoryHandler(e) { e.preventDefault(); const data = new FormData() data.append('csrfmiddlewaretoken',csrf[0].value) data.append('category',category.value) data.append('model',model.value) data.append('min_qnt',min_qnt.value) data.append('giga',giga.value) data.append('image',image.files[0]) $.ajax({ type: 'POST', url: …