Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
TinyMCE in Wagtail/Django project doesn't save some of the rich text features, like font size, alignments
I have a Django/Wagtail project. The project uses wagtailtinymce package. The TinyMCE looks great and it shows fine in all rich text fields and stream blocks. The Problem When I change the font size and save it. It is back to normal size as soon as the page refresh. so it doesn’t save the font size, and some other features too. However, it only saves a few features, like headings. Here is some code I have: requirements.txt ... wagtail==2.11.3 Django==2.2.16 wagtailtinymce @ git+https://github.com/TwilioDevEd/wagtailtinymce.git ... urls.py path('tinymce/', include('tinymce.urls')), ###settings INSTALLED_APPS = [ ... 'tinymce', 'wagtailtinymce' ] WAGTAILADMIN_RICH_TEXT_EDITORS = { 'default': { 'WIDGET': 'wagtaildashboard.models.MyTinyMCE' }, } wagtaildashboard.models.MyTinyMCE from django.db import models # Create your models here. from django.utils import translation from wagtailtinymce.rich_text import TinyMCERichTextArea from django.conf import settings class MyTinyMCE(TinyMCERichTextArea): @classmethod def getDefaultArgs(cls): return { "passthru_init_keys": { 'mode': 'textareas', 'plugins': 'preview importcss searchreplace autolink autosave save directionality code visualblocks visualchars fullscreen image link media template codesample table charmap pagebreak nonbreaking anchor insertdatetime advlist lists wordcount help charmap quickbars emoticons', 'editimage_cors_hosts': ['picsum.photos'], 'menubar': 'file edit view insert format tools table help', 'toolbar': 'undo redo | bold italic underline strikethrough | fontfamily fontsize blocks | alignleft aligncenter alignright alignjustify | outdent indent | numlist … -
redis-sentinel connection error in django with "AUTH <password> called without any password configured for the default user. Are you sure ..."
I have a problems with redis-sentinel connection. I tried to connect redis-sentinel by using django-redis. # redis connection factory DJANGO_REDIS_CONNECTION_FACTORY = 'django_redis.pool.SentinelConnectionFactory' # These sentinels are shared between all the examples, and are passed # directly to redis Sentinel. These can also be defined inline. SENTINELS = [ (env('REDIS_SENTINEL_IP_1'), 26379), (env('REDIS_SENTINEL_IP_2'), 26379), (env('REDIS_SENTINEL_IP_3'), 26379) ] CACHES = { "default": { "BACKEND": env('DJANGO_CACHE_BACKEND_REDIS'), # THE HOSTNAME IN LOCATION is the primary (service/master) name # example : redis://dev-redis/db # example : redis://staging-redis/db "LOCATION": env('DJANGO_CACHE_LOCATION_REDIS'), "OPTIONS": { # django_redis.client.SentinelClient "CLIENT_CLASS": env('DJANGO_CACHE_CLIENT_CLASS'), "SENTINELS": SENTINELS, "SENTINEL_KWARGS":{'password':env('REDIS_PASSWORD_VALUE')}, 'PASSWORD': env('REDIS_PASSWORD_VALUE'), "CONNECTION_POOL_CLASS": "redis.sentinel.SentinelConnectionPool", }, "KEY_PREFIX": "mapsvc" } } this is my settings.py. and my environment is on the kubernetes cluster and the redis-sentinel is the just node can access on the kubernetes cluster. I tried the connect to master in kubernetes pod. use this command redis-cli -h {master-ip} -p 6379 -a {password} and it works! but the django can't not connect to redis-sentinel. and it printed ERROR - create_cache: AUTH <password> called without any password configured for the default user. Are you sure your configuration is correct? the redis server version is 6.2.6. and django-redis version is 5.2.0. and I also tried without "CONNECTION_POOL_CLASS": "redis.sentinel.SentinelConnectionPool",settings in settings.py. please … -
How to use correctly types for Django model fields with django stubs?
Consider the following code fragment related to Django models: class Machine(HasStatus): # type: ignore machines: "models.Manager[Machine]" = models.Manager() number = models.IntegerField(verbose_name="Číslo stroje", unique=True) #Type of "number" is "IntegerField[Unknown, Unknown]" class Meta: #"Meta" overrides symbol of same name in class "HasStatus" verbose_name = "Machine" I was able to find out how to annotate Manager, but I don't know how to satisfy PyLance for model fields and Meta class. For the model fields, it seems I need to provide two types, but I have no idea which is the second. Please, provide examples of how to use type annotation for more Django model fields like ForeignKey and others. -
Django MSSQL query better way to fetch 100k records
I have a MSSQL database from where i need to fetch the records of about 100k. Then this record is shown in the dashboard and each operation in record is saved to the postgres DB which is the main DB of django. What is the best way to fetch the MSSQL server records, fetching everytime 100k records or is there is any way to store the query results to speed up the dashboard data view. -
Find Wagtail rootpage in users language
I have setup 2 rootpages according to https://wagtail.org/wagtail-localize/ English and Dutch For my menu system I have: {% get_site_root as site_root %} {% top_menu parent=site_root calling_page=self %} Which calls @register.simple_tag(takes_context=True) def get_site_root(context): return Site.find_for_request(context["request"]).root_page But it's always returning the Dutch root page. And when I'm on english it's not returning the english rootpage. Any ideas? -
control the empty data in django filter
I was coding the backend using Django. I am a beginner in Django. I used a filter to filter some post requests from the HTML form. here is the code. @api_view(["POST"]) def program_search(request): Data_List = [] MyFilter = CreateProgram.objects.filter(price__lte=request.POST['price'], days=request.POST['days']).values() ... but if I send a request from HTML form that one field of data be null the filter function cant handle it. -
Does a Django Model Serializer need performance tuning?
Is a Django serializer doing things like .only() on the same fields that are specified in the Serializer Meta: fields attribute or do I have to do it manually like saying in my view: def get_queryset(self) return SomeModel.objects.only('my_field_on_serializer') ? -
Django duplicate file to all the records with M2M which is self referencing
I have a Serializer & APIview where i upload a file it should be uploaded to other related records as well. class Record(models.Model): name = models.CharField(max_length = 122) related_records = models.ManyToManyField('self', null = True) class RecordDocument(models.Model): record = models.ForeignKey(Record, related_name = 'documents', null = True, blank= True ) file = models.FileField(max_length=255, blank=True, null=True) class RecordDocumentSerializer(serializers.ModelSerializer): save_documents_to_related_records = serializers.BooleanField() class Meta: model = RecordDocument fields = ( 'upload_to_related_cases', 'file' ) class RecordDocumentUploadView(APIView): def post(self, request, *args, **kwargs): 'how do i get serializers field **save_documents_to_related_records** and save the file on **related_records** which is not used in serializer till now if there are any inside it' -
DRF Invalid pk object does not exist for foreign key on POST create
I have two classes related to each other. One class I have made the primary key a char field so I can easily reference to it or create it to match the id of the actual object (all objects will have this unique name). from django.db import models class Ven(models.Model): id = models.CharField(max_length=80, primary_key=True) statusOn = models.BooleanField(default=True) class Device(models.Model): device_id = models.CharField(max_length=80) ven_id = models.ForeignKey(VEN, related_name='devices', on_delete=models.SET_NULL, null=True) class DeviceSerializer(serializers.ModelSerializer): class Meta: model = Device fields = ['id', 'device_id', 'ven_id'] class VENSerializer(serializers.ModelSerializer): class Meta: model = VEN fields = ['id', 'statusOn', 'devices'] class DeviceList(generics.ListCreateAPIView): logger.info("DeviceList: view") # permission_classes = [permissions.IsAuthenticated] queryset = Device.objects.all() serializer_class = DeviceSerializer however when I try to run my test: class DevicesTestCase(TestCase): def setUp(self): self.factory = Client() def test_create_device(self): device = { "ven_id": "TEST_VEN_1", "device_id": "TEST_DEVICE_1", "statusOn": True, } response = self.factory.post('/devices/', data=device) self.assertEqual(response.status_code, 201) my response returns 400 and states: b'{"ven_id":["Invalid pk \\"TEST_VEN_1\\" - object does not exist."]}' I'm trying to write a custom create in my serializer to create the ven if it does not exist but it's not being called. Data is being validated else where. My breakpoint in my view set's perform_create also does not fire. I don't want to write a … -
How to serialize data more efficiently in Django Rest Framework?
So guys, part of my problem is solved by the question: #72534250 I understood that the path I was following was wrong, and I should change the optimization of the models to serializers and/or viewsets. But how? I had this structure: *I already removed element_name and element_text from Models Foo class User(models.Model): name = models.CharField(max_lenth=50) age = models.IntergerField() class Customer(models.Model): user = models.ForeingKey(User, on_delete=models.CASCADE, verbose_name='User') class Element(models.Model): name = models.CharField(max_lenth=50) text = models.TextField() class Bar(models.Model): element = models.ForeingKey(Element, on_delete=models.CASCADE, verbose_name='Element') class Foo(models.Model): bar = models.OneToOneField(Bar, on_delete=models.CASCADE, verbose_name='Bar') customer = models.ForeingField(Customer, on_delete=models.CASCADE, verbose_name='Customer') is_active = models.BooleanField('Is Active', default=False) def user(self): return self.customer.user # Removed def element_name(self): return self.bar.element.name # Removed def element_text(self): return self.bar.element.text And these serializers: class UserSerializer(ModelSerializer): class Meta: model = User fields = '__all__' class CustomerSerializer(ModelSerializer): class Meta: model = Customer fields = '__all__' class ElementSerializer(ModelSerializer): class Meta: model = Element fields = '__all__' class BarSerializer(ModelSerializer): element = ElementSerializer() class Meta: model = Bar fields = '__all__' class FooSerializer(ModelSerializer): bar = BarSerializer() user = UserSerializer() class Meta: model = Foo fields = '__all__' And this viewset: class FooViewSet(ModelViewSet): serializer_class = FooSerializer permission_classes = [IsAuthenticated] authentication_classes = [JWTAuthentication] http_method_names = ['get', 'post', 'patch'] def get_queryset(self): active = self.request.query_params.get('is_active', False) … -
Django multitenant migration raises KeyError: "prune"
So I've been working in a project for a while, and haven't really changed the models at all, and therefore haven't done any migrations. Now I needed to add two new fields and delete another one, which should be normally fine. I'm using django-tenants, so my command to run the migrations is ./manage.py migrate_schemas. Now, whenever I run that, I get the error KeyError: "prune" (the full traceback is below) in what seems to be internal code of Django. Now, afterwards I tried reverting my changes, so no new migration and running the comnad again, and got the same error. I also thought that maybe the database had gotten "dirty" at some point, so I tried migrating a clean database from scratch, with the same result. The migrations don't even get to start. Has anyone ever encountered something similar? The full traceback (I have simplified the paths) [standard:public] === Starting migration Traceback (most recent call last): File ":directory/./manage.py", line 22, in <module> main() File ":directory/./manage.py", line 18, in main execute_from_command_line(sys.argv) File "$venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line utility.execute() File "$/lib/python3.9/site-packages/django/core/management/__init__.py", line 440, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "$venv/lib/python3.9/site-packages/django/core/management/base.py", line 402, in run_from_argv self.execute(*args, **cmd_options) File "$venv/lib/python3.9/site-packages/django/core/management/base.py", line 448, in execute output = … -
Why am I getting the error: "cannot read properties of null (reading "style") when I try to integrate Plaid link in JS?
My relevant code is as follows function useLink(token){ const handler = Plaid.create({ token: token, onSuccess: (public_token, metadata) => {}, onLoad: () => {}, onEvent: (eventName, metadata) => {}, recievedRedirectUri: window.location.href, }); } But everytime I try to use my link token to pull up link on the frontend, I get the following error from link-unitialize.js: Uncaught TypeError: Cannot read properties of null (reading 'style') I checked the Plaid website and couldn't find this listed as a common error, so I must be doing something very wrong. What is the most likely cause of this? -
Django - how to set user permissions after login via LDAP?
I'm starting a project in Django and I've already managed to get ldap authentication working, with django-auth-ldap. Now, I want that after the user logs in, django checks if this user exists in certain tables of my models, and if it does, give these users permission to access certain apps. For example, I have an app called 'alimentacao'. Only users registered in my model 'alimentacao.alunos' will be able to access "http://meu-projeto/alimentacao" As the user is created automatically if it doesn't already exist in the user model, I believe I would have to create a custom ldap authentication backend and check, after successful login, if the user exists in the table, and then set the permission to user. Neither the Django nor the django-auth-ldap documentation clearly explains how to create an authentication backend: in which folders the files need to be created, or how the application will call my custom method, or if I need to change any settings. I'm completely lost. -
using hyperlink inside a TextField
If one of my description model objects is being read as "This is a test". How do I attach a hyperlink to "test"? As in, when someone clicks on it, it takes them to test.com model description = models.TextField() admin class TestDescription(admin.ModelAdmin): formfield_overrides = { models.TextField: {'widget': TinyMCE()}, } -
Django, how to get Choices human friendly text for a choice value with no model instance
Firstly yes I know about get_FOO_display(). It's great if you have a model instance, I don't in my situation. Im sure every django user at some point wonders why Django uses tuples instead of a dict to define the choices, but anyways, I have this model: class Review(models.Model): PENDING = '' APPROVED = 'A' DECLINED = 'D' SKIPPED = 'S' RESULT_CHOICES = ( (PENDING, 'Pending'), (DECLINED, 'Declined'), (APPROVED, 'Approved'), (SKIPPED, 'Skipped'), ) result = models.CharField('result', max_length=1, choices=RESULT_CHOICES, blank=True, default='') Now I don't have a model instance, but I do have a value, e.g. A, or D. How do I get the matching text value, i.e. Approved, or Declined etc. I could build a dict, and then look up the value, but surely theres a more "Built in django" way? Not looking for this kind of hammer approach: human_friendly = [ PENDING: 'Pending', DECLINED: 'Declined', APPROVED: 'Approved', SKIPPED: 'Skipped', ]['A'] -
How do I apply Django middleware everywhere except for a single path?
I'm using Python 3.9 with Django 3. I have defined this middleware ... MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'corsheaders.middleware.CorsMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'directory.middleware.extend_token_response.ExtendTokenResponse' ] However, I don't want the middleware to apply to a certain URL. I have hard-coded this in the middleware like so class ExtendTokenResponse: def __init__(self, get_response): self.get_response = get_response # One-time configuration and initialization. def __call__(self, request): response = self.get_response(request) if request.path != '/' + LOGOUT_PATH: # Code to be executed for each request before # the view (and later middleware) are called. is_expired = True try: token = request.auth print("req path: %s" % request.path) is_expired = is_token_expired(token) if token else True except Exception as err: print(err) if not is_expired: but this seems a little sloppy and I would think the middleware comes with somethign out of the box to configure that this wouldn't need to be applied to my "/logout" path. Is there a more elegant way to configure this? -
With Django pagination my tables pages isn't same as the first page
I'm using pagination for my table in Django. In every page 8 rows of my table is showing. But when I go to second page its height and width isn't the same. All table cells height and width is changing. How can I fix this? My html: (a simple table) <table class="table" border=1> <thead> <tr> <th>Full Name</th> <th>Company</th> <th>Email</th> <th>Phone Number</th> <th>Note</th> <th>ID</th> </tr> </thead> <tbody> {% for customer in customers %} <tr> <td>{{customer.full_name}}</td> <td>{{customer.company}}</td> <td>{{customer.email}}</td> <td>{{customer.phone_number}}</td> <td>{{customer.note}}</td> <td>{{customer.id}}</td> </tr> {% endfor %} My View class TableView(ExportMixin, SingleTableView): model = Customer table_class = CustomerTable template_name = 'pages/user_page.html' context_object_name = 'customers' paginate_by = 8 My Css .table{ margin-top:60px; position: fixed; top:250px; left:100px; bottom:250px; right:100px; width:900px; margin-left: 230px; } -
Django: The QuerySet value for an exact lookup must be limited to one result using slicing using django
i am writing a logic that would allow me display all the quizes that a teacher have created from the database, now i want to make this more precise by only showing a student a quiz based on the courses they enrolled in, i am trying to write this filter and it not working as expected models.py Class Course(models.Model): course_creator = models.ForeignKey(User, on_delete=models.CASCADE, related_name="quiz_user") course_title = models.CharField(max_length=255) slug = models.SlugField(unique=True) def __str__(self): return self.title class UserCourse(models.Model): user = models.ForeignKey(User , null = False , on_delete=models.CASCADE) course = models.ForeignKey(Course , null = False , on_delete=models.CASCADE, related_name="usercourse") payment_status = models.CharField(max_length=100, choices=PAYMENT_STATUS, default="pending") date = models.DateTimeField(auto_now_add=True) class Quiz(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="quiz_user") title = models.CharField(max_length=255) course = models.ForeignKey(Course, on_delete=models.SET_NULL, null=True) date = models.DateTimeField(auto_now_add=True) slug = models.SlugField(unique=True) def __str__(self): return self.title views.py def StudentQuizList(request): user_course = UserCourse.objects.filter(user=request.user) quizzes = Quiz.objects.filter() context = { # 'user_course': user_course, 'quizzes': quizzes, } return render(request, 'student_dashboard/quiz.html', context) error The QuerySet value for an exact lookup must be limited to one result using slicing. -
how can i persist data on website in django?
i have just started using django and dabbling with front end technologies i wrote a small web app and i want to push this up on a host server. my question is how do i update the website without losing the data in the dblite and user tables? won't i overwrite the db? this concept confuses me a little bit. i understand how this would work if i had a backend app servicing the webaite but this is going to be a website just for me and a few friends and family members. ... -
Validation isn't working for SerializerMethodField
I am working on my Django DRF app. I have two models Organization and BankAccount class Organization(models.Model): ... class BankAccount(models.Model): is_main = models.BooleanField(default=False) # organization should have 1 main account organization = models.ForeignKey( Organization, related_name="accounts", on_delete=models.CASCADE, ) ... I want to validate Organization data Organization must have at least one account with is_main=True but my validation isn't working (it looks like there are no validate_accounts cals. class OrganizationSerializer(serializers.ModelSerializer): accounts = serializers.SerializerMethodField() class Meta: model = Organization fields = '__all__' read_only_fields = ("id",) def get_accounts(self, obj): if obj.accounts is not None: return BankAccountSerializer(obj.accounts, many=True).data return None def validate_accounts(self, value): """ Organization should have account with 'is_main' true """ raise ValueError(value) # Don't see exception in logs if not value: return value for account in value: # check all accounts if account["is_main"] == True: return value raise serializers.ValidationError("Organization should have main account (is_main=true)") Example: For this data I expect validation error: (only one acc with "is_main": false,) "accounts": [ { "id": 1, "is_main": false, "bank_name": "Sber", "bank_bik": "123", "bank_account": "123", "correspondent_account": "312", "organization": 3 }, ] -
How to use mapbox vector tiles in a performant way?
I am a bit confused about Mapbox MVT. As I understood, a tile is a little piece of map, as in a jigsaw puzzle. Not completely sure about the working of MVT. https://docs.mapbox.com/data/tilesets/guides/vector-tiles-introduction/#benefits-of-vector-tiles Here, it says Vector tiles are really small, enabling global high resolution maps, fast map loads, and efficient caching. So the thing is I am trying to get all the coordinates from db which can go up to more than 10K and currently getting the data from postgis using: query = f""" SELECT NULL AS id, ST_AsMVT(q, 'default', 4096, 'mvt_geom') FROM (SELECT "locations"."name", ST_AsMVTGeom(ST_Transform(locations.geom, 3857), ST_TileEnvelope(1, 0, 0), 4096, 0, false) AS mvt_geom FROM locations) AS q; """ with connection.cursor() as cursor: cursor.execute(query) rows = cursor.fetchall() mvt = bytes(rows[-1][-1]) return Response( mvt, content_type="application/vnd.mapbox-vector-tile", status=200 ) Now I am wondering about the performance issues, as everytime a user will visit it will put stress on db. And another problem I am having is when using vector tiles as a source, it calls the source url and (hitting the db) everytime I move the map. type: 'vector', tiles: [ 'http://url/{z}/{x}/{y}.mvt' ] Is it possible to call the source url at a specific zoom level and until then all the … -
django.db.utils.IntegrityError: (1048, "Column 'user_id' cannot be null") in DRF
I have an app and I use django.contrib.auth.models.User as authentication models then I use for serializer: class UserSerializer(serializers.ModelSerializer): class Meta: model = User fields = '__all__' # fields = ['first_name', 'last_name', 'email', 'password', 'username'] def validate(self, attrs): if 'name' in attrs.keys(): name = attrs['name'] name = name.split(' ') first_name, last_name = name[0], name[1] attrs['first_name'], attrs['last_name'] = first_name, last_name attrs['password'] = make_password(attrs['password']) return attrs then I use for view set: class UserViewSet(viewsets.ViewSet): permission_classes = (permissions.AllowAny,) # authentication_classes = (TokenAuthentication) ... def create(self, request): # return response.Response(data=request.data) # user = User.objects.filter(username=request.data['username']).exists() # return response.Response(data= user) if User.objects.filter(username=request.data['username']).exists(): serials = UserSerializer( data=request.data, context={'request': request} ) user = None if serials.is_valid(): user = serials.save() else: user = UserSerializer(request.data, many=True) token, created = Token.objects.get_or_create(user=user) return response.Response( data={ 'token': token.key, 'user': UserSerializer(user).data } ) ... And after that I got this error: django.db.utils.IntegrityError: (1048, "Column 'user_id' cannot be null") -
Allow data insertion only after time duration threshold in django
I have a model in Django with a DateTimeField attribute. I want to forbid insertion of new data in the database if the duration between the datetime field of the new data and the latest datetime field in the database is less than some duration threshold. class MyModel(models.Model): time_stamp = models.DateTimeField(default=timezone.now, null=True) When I want to insert a datapoint say today, and the latest time stamp in my database is yesterday, and the duration threshold is one month (this operation should not be possible). -
Django form's redirect me to the wrong URL if the input data is invalid
I am very new to Django and working on a marketplace site. I have a few forms on my main listing page, one for taking bids and another for leaving comments. The bidding form when given a bid that is too low is supposed to redirect the used to the listing page now with an error displayed. What actually happens is that the URL changes and it displays a different view of the same listing page. I'm sure this can be solved easily with JavaScript and ignore the redirect all together, but I'd rather figure out why this is happening then try to work around it. When entering invalid data I expect to be taken back to my current page "listing/product_name", but instead I am taken to "bid/product_name". The same thing happens with the comments form "/comment/product_name", that form doesn't want to validate either. URLS from django.urls import path from . import views urlpatterns = [ path("", views.index, name="index"), path("ended", views.ended, name="ended"), path("login", views.login_view, name="login"), path("logout", views.logout_view, name="logout"), path("bid/<str:listing>", views.bid, name="bid"), path("register", views.register, name="register"), path("watchlist", views.watchlist, name="watchlist"), path("close/<str:listing>", views.close, name="close"), path("newListing", views.newListing, name="newListing"), path("listing/<str:listing>", views.listing, name="listing"), path("comment/<str:listing>", views.comment, name="comment"), path("change_watchlist/<str:listing>", views.change_watchlist, name="change_watchlist"), ] views.py I know there is some messiness … -
How i can show image from database to page. Django
i have media directory in my project, and i can't take image from it. When i try getting image, then i got this Request URL:http://127.0.0.1:8000/auto/media/img/'MyImg'.jpg and obviously it can't work, so how can i do this? while doing this i am trying to get data in html like this {% for item in auto %} <div> <p>{{item.name}}</p> <p>{{item.info}}</p> <p>{{item.description}}</p> <img src="media/{{item.photo}}"> </div> <hr> {% endfor %}