Categories
Mastering Development

how can i slice a list with override save models.py [:2]

how can i slice a list with override save models.py i also used this code in models.py field this code is working but slicing [:2] def save(self, *args, **kwargs): if self.featured == True: Article.status_objects.filter(featured=True).update(featured=False) self.featured = True super(Article, self).save(*args, **kwargs) when i try to slice list with override save method my server get error ‘int’ […]

Categories
Mastering Development

Django uploading image in custom template

forms.py from django import forms from .models import VideoPost class PostForm(forms.ModelForm): class Meta: model = VideoPost fields = (‘category’, ‘title’, ‘slug’, ‘content’, ‘video’, ‘image’,) models.py class VideoPost(models.Model): category = models.ForeignKey(‘Category’, on_delete=models.CASCADE) title = models.CharField(max_length=100) slug = models.SlugField(max_length=100, unique = True) author = models.ForeignKey(User, on_delete=models.CASCADE) video = models.CharField(max_length=100, blank=True) content = RichTextUploadingField() image = models.ImageField(upload_to=’images’, null=True, […]

Categories
Development

draw multiple text on image with PIL django python

currently its drawing one text on thumbnail and i want to draw multiple watermark on image thumbnail models.py: class Image(models.Model): license_type = ( (‘Royalty-Free’,’Royalty-Free’), (‘Rights-Managed’,’Rights-Managed’) ) image_number = models.CharField(default=random_image_number,max_length=12,unique=True) title = models.CharField(default=random_image_number,max_length = 100) image = models.ImageField(upload_to = ‘image’ , default = ‘demo/demo.png’) thumbnail = models.ImageField(upload_to=’thumbs’, blank=True, null=True) category = models.ForeignKey(‘Category’, null=True, blank=True, on_delete=models.CASCADE) shoot […]

Categories
Development

how to automatically generate a csv file when a user press the search button

i have a class base view which i want to automatically download a csv file but dont seem to know what am doing. here is code Model.py class Pv(models.Model): accounts =( (‘Sub CF’,’Sub CF’), (‘Special’,’Special’), (‘Directors’,’Directors’), (‘Operations’,’Operations’), (‘LSGDP’,’LSGDP’), (‘DWAP’,’DWAP’), (‘Capacity(USD)’,’Capacity(USD)’) ) acc =( (‘Yes’,’Yes’), (‘No’, ‘No’) ) source =( (‘GOG’,’GOG’), (‘Others’, ‘Others’) ) pv =( […]

Categories
Development

How to index just selected json key using Django functionality instead of all keys?

I have the next model: from django.contrib.postgres.fields import JSONField from django.contrib.postgres.fields.jsonb import KeyTextTransform from django.contrib.postgres.indexes import GinIndex from django.contrib.postgres.search import SearchVectorField, SearchVector from django.db import models class ProfileUser(models.Model): name = JSONField() search_vector = SearchVectorField(null=True) class Meta: indexes = [GinIndex(fields=[“search_vector”], name=”user_full_name_gin_idx”)] def save(self, *args, **kwargs): super(ProfileUser, self).save(*args, **kwargs) ProfileUser.objects.filter(pk=self.pk).update(search_vector=SearchVector(‘name’)) # I have tried this, but this […]

Categories
Development

AttributeError: ‘tuple’ object has no attribute ‘_committed’

Full traceback: Traceback (most recent call last): File “crawl_ebaumsworld.py”, line 92, in <module> crawl(first_url) File “crawl_ebaumsworld.py”, line 66, in crawl creator=mike File “/home/michael/projects/deepsteg/venvdeepsteg/lib/python3.6/site-packages/django/db/models/manager.py”, line 82, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File “/home/michael/projects/deepsteg/venvdeepsteg/lib/python3.6/site-packages/django/db/models/query.py”, line 422, in create obj.save(force_insert=True, using=self.db) File “/home/michael/projects/deepsteg/engine/models.py”, line 62, in save return super(AttackImage, self).save(*args, **kwargs) File “/home/michael/projects/deepsteg/venvdeepsteg/lib/python3.6/site-packages/django/db/models/base.py”, line 741, in save […]

Categories
Development Django

Am I overriding the save method on the model in a wrong way in django?

I have a Profile model that extends the user model like so, class Profile(User): user = models.OneToOneField(User, parent_link=True, on_delete=models.CASCADE) slug = models.SlugField(unique=True, blank=True) def save(self, *args, **kwargs): print(‘self.username’) print(self.username) self.slug = self.username super(Profile, self).save(*args, **kwargs) I am trying to create a slug field for my model , so I override the save method to include […]

Categories
Development Django

Am I overriding the save method on the model in a wrong way in django?

I have a Profile model that extends the user model like so, class Profile(User): user = models.OneToOneField(User, parent_link=True, on_delete=models.CASCADE) slug = models.SlugField(unique=True, blank=True) def save(self, *args, **kwargs): print(‘self.username’) print(self.username) self.slug = self.username super(Profile, self).save(*args, **kwargs) I am trying to create a slug field for my model , so I override the save method to include […]

Categories
Development Django

How to save multiple ManytoMany instances to a Django object?

I have a django app with several Question objects. Question and Quiz are related through a ManyToManyField. I want to create Quiz objects such that each quiz is randomly assigned 3 questions. I’ve tried to create a save method in my model where I: use a queryset to get 3 questions, save the Quiz instance […]

Categories
Development

how to make the models caseinsensitive

I have a model. class Exam(models.Model): Examname = models.CharField(null=False, blank=False, max_length=255) def save(self, *args, **kwargs): self.Examname = self.Examname.lower() return super(Exam, self).save(*args, **kwargs) class Meta: unique_together = [“Examname”] def __str__(self): return self.Examname Examname must be case insensitive in order to avoid the duplicate data. I converted all to small letters and stored. But it is not […]