Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django Fixtures minimal subset that respects foreign key constraints
I want to set up a test DB for my Django project in order to do some propert testing. Sadly our DB is huge and very interconnected. This means using the full DB for testing is very slow if django needs to import a fixture with millions of lines of data at the start. However creating a suitable subset of Data seems to be problematic too, because there are so many foreign key constraints. I used this script to create a fixture: from itertools import chain from django.db.utils import DEFAULT_DB_ALIAS from django.core import serializers from django.contrib.admin.utils import NestedObjects from models import EntryTable collector = NestedObjects(using=DEFAULT_DB_ALIAS) # database name collector.collect([EntryTable.objects.get(pk=1)]) objects = list(chain.from_iterable(collector.data.values())) objects.reverse() with open("search/fixtures/TestSkeleton.json", "w") as f: f.write(serializers.serialize("json", objects)) However it seems that this does not resolve all dependencies. For example EntryTable is pointed to by a Table A, and A is pointed to by a table ALinkB, which also points to a table B. The relevant Objects in A and ALinkB are in the fixture however table B was not. Therefore loading this fixture resulted in violated foreign key constraints. Does someone know what I am missing? -
why urls will append after last url in django
when i click on on about or another tabs for first time everything is good but when i click a tab again after another tab its url appends to last url.strong text urls: from .views import * app_name = 'root' urlpatterns = [ path("",home,name='home'), path("about/",about,name='about'), path("contact/",contact,name='contact'), path("trainers/",trainers,name='trainers') ]``` html: ``` <li><a class="active" href="{% url 'root:home' %}">Home</a></li> <li><a href="{% url 'root:about' %}">About</a></li> <li><a href="{% url 'course:courses' %}">Courses</a></li> <li><a href="{% url 'root:trainers' %}">Trainers</a></li> <li><a href="{% url 'root:contact' %}">Contact</a></li>``` ------------------------------------------------------------------------ -
Creating a django model for a completely different set of users
I created a model called Driver, which isn't inheriting from the in-built User model every django project comes with. Another model I created inherits from it. Each driver has a name, email and phone number. The model doesn't inherit the set_password() or check_password() functions from the User model so I had to make some additions to the model to save the hashed password. However, any time I test a login, it doesn't work, on account of the typed password not matching the hash in the database. from django.db import models from django.contrib.auth.hashers import make_password class Driver(models.Model): name = models.CharField(max_length= 50) phone_number = models.CharField(max_length=15) email = models.EmailField(unique=True, null=True) password = models.CharField(max_length=50) def __str__(self): return self.name def save(self, *args, **kwargs): # Hash the password before saving self.password = make_password(self.password) super().save(*args, **kwargs) def check_password(self, password): return self.password == password How can I create a function to properly compare the typed passwords to the hashed version in the database? Or am I going about this all wrong? -
Django : Streaming data with PyPDF2
When the pdf is generate , i would like to avoid saving a file to disk : output_pdf = PdfWriter() for i in range(len(input_pdf.pages)): page = input_pdf.pages[i] if i == 0: page.merge_page(text_pdf.pages[0]) output_pdf.add_page(page) with open('assets/media/consignes.pdf', 'wb') as f: output_pdf.write(f) f.close() return FileResponse(open('assets/media/consignes.pdf','rb'), as_attacheement=False, content_type='application/pdf') -
Django rest framework add image with react and axios
I am trying to make a form where a user uploads a post which is just a picture. I am using django rest framework for api and react for frontend. const formData = new FormData() const savePost = () => { axios.post('http://127.0.0.1:8000/create_post/', formData, { headers: { "Content-Type": "multipart/form-data", }, }) } const handleChange = (e) => { if (e.target.files && e.target.files.length > 0) { setSelectedFile(e.target.files[0]) formData.append("image", e.target.files[0]); } <form onSubmit={savePost}> <input type="file" onChange={handleChange} /> <input type="submit" /> </form> But when I press submit a new instance is added but the image is just /media/ without the actual image. -
Testing new django database migrations on filled live database or freshly created one?
I'm currently working on a django project running on docker with an sqlite3 database. We are using Gitlab as Version Control (including gitlab CI system). Im just testing for now and we have not deployed yet/are not live, but i have some issues thinking of the final workflow after deploying. Regarding the CI system, i was searching for a while now, but i didn't find an accurate answer on how the migrations and the database itself are tested in the CI system and how a proper development workflow looks like. Im mainly concerned with the following point: Since all our migration files are under version control from the first start of the database (we will start with a fresh db.sqlite when going live). Is it then necessary to check every following migration on the (filled) live database (or latest backup?)? Or can i just create a fresh database for every migration check (when model changes are made). To give an example maybe: Im a developer and make changes to some models of the project. Is it ok now for testing to create a fresh database, then firstly apply the old migrations to the database, then 'makemigrations' for the new changes … -
save_screenshot() vs get_screenshot_as_file() in Selenium in Python
I took 2 screenshots of Django Admin with save_screenshot() and get_screenshot_as_file() as shown below. *I use Django, pytest-django and Selenium: def test_1(live_server): driver = webdriver.Chrome() driver.get(("%s%s" % (live_server.url, "/admin/"))) driver.save_screenshot("admin.png") # Here def test_1(live_server): driver = webdriver.Chrome() driver.get(("%s%s" % (live_server.url, "/admin/"))) driver.get_screenshot_as_file("admin.png") # Here Then, I got the same screenshots as shown below: admin.png: So, what is the difference between save_screenshot() and get_screenshot_as_file() in Selenium in Python? -
How to deal with unresolved related model during test migrations
I have an app in production that is very okay. I am currently adding unit tests but so far the tests are failing because of a migration-related error during the creation of the test database. ValueError: Related model 'my_app.my_model' cannot be resolved Here is the full Stack Trace Some background info: I had a model MyModel that was a ForeignKey on other models in different apps. I renamed the model to NewModel. I also renamed all the ForeignKeys from my_model to new_model. Django migrations asked whether I renamed the model, I accepted, and I have verified the renaming was done correctly in the migration files. My test case is something like this: class DoerTestCase(TestCase): @classmethod def setUpTestData(cls): A.objects.create(title='Test A') NewModel.objects.create(title='Test New Model') def setUp(self): a = A.objects.first() new_model = NewModel.objects.first() B.objects.create(a=a, new_model=new_model) def test_doer(self): b = B.objects.first() ... How can I resolve this without possibly interfering with the existing migrations? -
error when build docker for django and mysql
I tried to build docker. The docker include a django project, the project need to use MySQL. but fail to build when I use commend sudo docker compose up with error message 1.979 django.db.utils.OperationalError: (2005, "Unknown MySQL server host 'mysql' (-2)") ------ DEBU[0000] using default config store "/root/.docker/buildx" DEBU[0000] serving grpc connection DEBU[0000] stopping session span="load buildkit capabilities" DEBU[0000] serving grpc connection DEBU[0004] stopping session failed to solve: process "/bin/sh -c python /app/manage.py migrate" did not complete successfully: exit code: 1 python version: 3.7.16 (required) django version: 3.2 (required) ubuntu: 22.04.3 docker version: 24.0.6 docker compose version: v2.21.0 settings.py from pathlib import Path from datetime import timedelta from django.core.management.utils import get_random_secret_key BASE_DIR = Path(__file__).resolve().parent.parent SECRET_KEY = get_random_secret_key() DEBUG = True ALLOWED_HOSTS = [] INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', 'rest_framework_simplejwt', 'project', ] REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework_simplejwt.authentication.JWTAuthentication', ), } 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', ] ROOT_URLCONF = 'project.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] WSGI_APPLICATION = 'project.wsgi.application' DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'articles', 'USER': 'root', 'PASSWORD': '1234', 'HOST': 'mysql', 'PORT': '3306', … -
Compiling SCSS/SASS in django in production environment?
I'm not an expert in Django, but I was reading here and found that there's not much I can do to compile SASS in production environment. Could you suggest some way to compile SASS/SCSS in production environment? Can node JS and python run simultaneously on VPS so that sass can be compiled? -
Need help in creating a beginner project
I really need some help, so I am learning Django and getting an opportunity in my current job to get a role as a backend developer. I am still at the beginner level. I know how to make basic projects and stuff but now my employer has asked me to make a project as follows- Create a User authentication model using JWT and REST Framework Then one admin and other users are to there. Using CURD I need to allow the users to add sales in the project with relevant fields. In the end the admin should be able to see all user profile sales and should be able to edit. While A user should just be allowed to edit or see his own sales. Can you please help me? I am not able to get a hold of how to implement JWT token and create the start of it. How will I be making the model. Because as much knowledge I have gained it's about creating a superuser creating a user form and then allowing them to add their profiles. But doing it via API in REST FRAMEWORK is somehting I need help on. -
Using s3Boto3Storage with CompressedManifestStaticFilesStorage
I'm working with the WhiteNoise package and need to create a custom class that utilizes the methods provided by CompressedManifestStaticFilesStorage while using S3Boto3Storage for storage, without relying on StaticFilesStorage. How can I achieve this in my Django project? class CompressedManifestStaticFilesStorage(ManifestStaticFilesStorage): # methods class ManifestStaticFilesStorage(ManifestFilesMixin, StaticFilesStorage): """ A static file system storage backend which also saves hashed copies of the files it saves. """ pass I dont' want to use StaticFilesStorage. -
Django formset queryset issue
I have a formset dynamic with form (model.form), and it work if I set only one form, if a add a new one, the previous one seems not filtered, only the last form is filtered. How can make a multi filtered ? forms.py class CmsFilterForm(ModelForm): class Meta: model = Cmsexport fields = ['name','state_text'] class DnsFilterForm(ModelForm): class Meta: model = Dns fields = ['dns_name', 'dns_domain'] CmsFilterFormSet = formset_factory(CmsFilterForm, extra=1) DnsFilterFormSet = formset_factory(CmsFilterForm, extra=1) views.py: FORM_MAPPING = { 'cms': CmsFilterForm, 'dns': DnsFilterForm, } FORMSET_MAPPING = { 'cms': CmsFilterFormSet, 'dns': DnsFilterFormSet, } class Table(ListView): template_name = 'table.html' context_object_name = 'table' paginate_by = 10 def get_queryset(self): model = apps.get_model('dashboard', MODEL_MAPPING[self.kwargs['name']]) queryset = model.objects.all().values() field = self.request.GET.get('field') lookup = self.request.GET.get('lookup') value = self.request.GET.get('value') reset = self.request.GET.get('btn-reset') if field and lookup and value: query = field.lower().replace(" ", "_") + '__' + lookup print(query) queryset = model.objects.filter(**{ query: value }).values() if reset: queryset = model.objects.filter().values() return queryset def get_context_data(self, **kwargs): context = super(Table, self).get_context_data(**kwargs) context['form'] = FORM_MAPPING[self.kwargs['name']] context['formset'] = FORMSET_MAPPING[self.kwargs['name']] return context template: <form method="GET"> {{ formset.management_form }} {% for form in formset %} <div class="link-formset"> <label for="Field">Field </label> <select name="field" class="form-control"> {% for field in form %} <option value="{{field.label}}">{{field.label}}</option> {% endfor %} </select> <label for="lookup">Lookup </label> … -
how to insert raw html to django-cms page using cms.api?
I am using django-cms and their cms.api to migrate some articles from a WordPress site. So far I have managed to: import requests as req from cms.api import create_page, add_plugin from bs4 import BeautifulSoup as bs from datetime import datetime raw = req.get("site_base/article/").text soup = bs(raw) title = soup.find("title").text slug = soup.find("meta", {"property":"og:url"})['content'].split("site_base")[1].\ replace("/","") meta_desc = soup.find("meta", {"name": "description"})['content'] page = create_page(title, template='home.html', language='en', slug=slug, meta_description=meta_desc, created_by='Sid', publication_date=datetime.now().date()) placeholder = page.placeholders.get(slot='content') add_plugin(placeholder, 'TextPlugin', 'en', body='hello world') I am trying to find a way to put the raw html of the body(I have the articles in html format, if that matters) as content. I have looked up all possible resources I could think of and nothing is working. -
Adding products to Shopify with MEDIA (3d models, images ....) using ShopifyAPI - DJANGO
Hope you're having a wonderful day! I'm working on an a Django project that uses ShopifyAPI using this project as base: https://github.com/Shopify/shopify_django_app, i was able to Authenticate, see and add products, My problem is that i couldn't add media to the products using the api (for example .glb files for 3d models) in order to preview it in the store website. I want to be able to create products and assign all information using my view. i create products like this: new_product = Product() new_product.title = title new_product.tags = ",".join(tags) new_product.product_type = product_type new_product.vendor = vendor price = self.request.data.get('price', '50.00') sku = self.request.data.get('sku', 'Default-SKU') variant = Variant() variant.price = price variant.sku = sku new_product.variants = [variant] new_product.save() I reasearch some solutions but i couldn't find any, except maybe for using metafields and that's how i tried it but with no use! metafield = Metafield({ "namespace": "digital", "key": "file_url", "value": file_url, "value_type": "string", "description": "Download link for the digital product", "owner_resource": "product", "owner_id": new_product.id }) metafield.save() how can i attach media to products from my django view? thanks in advance -
Enum values not enforced
In Django, I'm trying to create a enum-based database field. I'm avoiding external libraries and want to only use vanilla Django4. Essentially, I'm replicating the example given in Python - Covert enum to Django models.CharField choices tuple However, the enum values are not enforced. I can easily create a database entry with animal = 'foo' and it happily writes this into the database. I am expecting this to yield an IntegrityError, since I'm explicitly using an enum so only valid values end up in the db. -
Django Rest API: AttributeError: 'function' object has no attribute 'get_extra_actions'
I try to make an endpotint to add new records in db with a post request but I receive this error: AttributeError: 'function' object has no attribute 'get_extra_actions' my views.py class newUser(generics.ListCreateAPIView): def createUser(self, request): name: request.POST['name'] phone: request.POST['phone'] email: request.POST['email'] newUser = User(name=name, phone=phone, email=email) User.save() return newUser my routes router = routers.DefaultRouter() router.register(r'user',UserViewSet, basename='user') router.register(r'createuser',newUser.as_view(), basename='createuser') urlpatterns = [ path('admin/', admin.site.urls), # path('api/users/', UserViewSet.as_view({'get': 'list'})), ] urlpatterns += router.urls -
Can I modify Django form data and post back to the page?
I want to use a django form, update some of the fields on POST (to the same page) and display the updated field values. There is no db involved here. I have a Django form. I am following the documentation here https://docs.djangoproject.com/en/4.2/ref/forms/) Here is my view code: def QuestionView(request): # if this is a POST request we need to process the form data if request.method == "POST": # create a form instance and populate it with data from the request: form = QuestionForm(request.POST) # check whether it's valid: if form.is_valid(): query = form.data["question"] result = chat(filepath=filepath, query=query) form.cleaned_data["answer"] += query result["answer"] form.cleaned_data["confidence_score"] = result["score"] # if a GET (or any other method) we'll create a blank form else: form = QuestionForm() return render(request, "home.html", {"form": form}) Here is my form code: class QuestionForm(forms.Form): question = forms.CharField(label="Question", required=1, max_length=255) answer = forms.CharField(label="Answer", required=0, max_length=16384) confidence_score = forms.IntegerField(label="Confidence", required=0, min_value=0, max_value=100)``` The line result = chat(filepath=filepath, query=query) is an external function that successfully returns an array of values. I can assign these values successfully to the form.cleaned_data. I cannot then render the updated cleaned_data back to the form. Am I going completely down the wrong route or have I just missed an … -
checkbox in list view in django template , retrieve all checked box objects in each pagination to view of Django (server)
I have checkbox in list view and contains pagination in html template. I want to retrieve the all checked box objects in each pagination to server (view part of Django) . Example in page 1 , if i checked 4 objects , page2 checked 3 objects , page 3 checked 4 objects then finally when i clicked then i will get all 11 checked objects into the server. I tried , But i am getting only selected objects in current page . When i navigate to next page then first selected object deleted . I don't know how to retrieve all objects together in each page . -
Django django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet
I have an app for surveys. I am trying to import my model in views.py and its giving an error. My code is: settings.py: INSTALLED_APPS = [ 'survey.apps.SurveyConfig', 'survey.urls', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ] urls.py: from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('survey/', include('survey.urls')), ] survey/urls.py: from django.urls import path from . import views urlpatterns = [ path('', views.list_surveys, name='list-surveys'), path('create/', views.create_survey, name='list-surveys'), ] survey/views.py: from django.shortcuts import render from .models import MultipleChoiceQuestion surveys = [ { "id": "S001", "questions": [ ["How much do you like Userfacet?", 1], ["How much do you like frontend?", 0], ["How much do you like backend?", 2], ["How much do you like fullstack?", 0], ["How much do you like Userfacet?", 3], ["How much do you like frontend?", 0], ["How much do you like backend?", 4], ["How much do you like fullstack?", 0], ["How much do you like Userfacet?", 5], ["How much do you like frontend?", 0], ["How much do you like backend?", 6], ["How much do you like fullstack?", 0], ["How much do you like Userfacet?", 7], ["How much do you like frontend?", 0], ["How much do you like backend?", 8], ["How much do you like … -
How to solve CreateAPI 400 error in this DjangoRestFramework in Serializer Create Method?
I have a model, Doctor, that has a Foreign key relation to the user model. I'm handling the creation of Doctor instances through the serializer of the Doctor Model. The flow I'm going for is simply to check if a user with the given username exists. If it exists, then create a new Doctor Instance with the existing user, if it doesn't, then first create a new user, and then create a Doctor Instance with that new user. Here is my Doctor Serializer :-> class StaffSerializer(serializers.ModelSerializer): """ Serializer for viewing entries Doctor model """ departments_display = DepartmentSerializer( many=True, source="departments", read_only=True ) appointment_types_display = AppointmentTypeSerializer( many=True, source="appointment_types", read_only=True ) user = UserSerializer() permissions = PermissionSerializer(many=True) doc_image = serializers.SerializerMethodField() class Meta: model = Doctor exclude = ("hospitals",) extra_kwargs = { "departments": {"write_only": True}, "appointment_types": {"required": False}, } def get_doc_image(self,obj): return obj.doctor_image def create(self, validated_data): """ Create and return a new `Doctor` instance, given the validated data. """ # try: with transaction.atomic(): user_data = validated_data.pop("user") try: user = User.objects.get(username = user_data["username"]) validated_data["user"] = user except ObjectDoesNotExist: serializer = UserSerializer(data = user_data) if serializer.is_valid(): serializer.save() validated_data["user"] = serializer.instance permmissions = validated_data.pop("permissions") permissions_to_insert = [] for permission_entry in permmissions: uiserializer = UIElementSerializer(data = permission_entry["ui_element"]) uiserializer.is_valid() … -
is there an option to display a selenium session in website
Is it possible to display a Python selenium session (which run a flow in some website) on my website so that the user can see the session that is running? I'm using the Django library. -
Check constraint referencing fields of database object
Let's say I have a Django object like this: class X(models.Model): start_main = models.DateField("start X") And another like this: class Y(models.Model): x = models.ForeignKey(X, on_delete = models.CASCADE) start_sub = models.DateField("start Y") I want to add a CheckConstraint to Y that always ensures that Y.start_sub >= Y.x.start_main. Is this possible? I've looked into django.core.validators.MinValueValidator for example but that only takes concrete values as parameter. I've also looked into the check = models.Q(...) syntax but am unable to get syntactically correct constraint. -
conditions on django generic detailview
I'm trying to show a post detail view only when post.status=='pub'. I did it in list view with overriding get_queryset method but how can i do the same with detail view? I want the detail view to return 404 when post.status=='drf' models.py class Post(models.Model): STATUS_CHOICES = ( ('pub', 'عمومی'), ('drf', 'پیش نویس') ) title = models.CharField(max_length=60, null=False, blank=False, verbose_name='عنوان') description = models.TextField(max_length=200, verbose_name='توضیحات') body = RichTextField(verbose_name='بدنه') author = models.ForeignKey(get_user_model(), models.CASCADE, verbose_name='نویسنده') datetime_created = models.DateTimeField(auto_now_add=True, verbose_name='زمان ایجاد') datetime_modified = models.DateTimeField(auto_now=True, verbose_name='آخرین ویرایش') status = models.CharField(choices=STATUS_CHOICES, max_length=3, verbose_name='حالت انتشار') class Meta: verbose_name = 'پست' PostListView and PostDetailView class PostListView(generic.ListView): model = models.Post context_object_name = 'posts' template_name = 'pages/post_list.html' def get_queryset(self): queryset = super(PostListView, self).get_queryset() return queryset.filter(status='pub') class PostDetailView(generic.DetailView): model = models.Post context_object_name = 'post' template_name = 'pages/post_detail.html' -
django URL in a TextField with HTML
In my Django e-commerce project, I have an obvious Product model with an expected description field: description = models.TextField(...) I have the following url configured: path("product/slug:slug", views.product_detail, name="product_detail"), which, obviously, serves URLs like this: http://localhost:8000/product/foo and when a product foo is a part of a set with product bar, I want to add respective links to the description field on such products, like "you might want to buy bar together with this". However, adding a link to bar to the description of foo proved tricky. The whole description field (imported from a legacy ecomm app) can contain arbitrary HTML, and since it's not a user input, I render it like this in the template: {% autoescape off %} {{ product.description }} {% endautoescape %} When I add this to the (TextField) description: > This item together with <a href="product/bar">Bar</a> (sold separately) , the URL produced in the template is wrong: http://localhost:8000/product/product/bar/ , but if I don't use this '/product' in the description's url: > This item together with <a href="/bar">Bar</a> (sold separately) make > a matching set. , the URL is also wrong: http://localhost:8000/bar/ I'll admit, I don't understand how Django constructed the first or second URL. So it's ironic …