django-models

Save Base64 String into Django ImageField

空扰寡人 提交于 2021-02-07 12:15:32
问题 Im doing an application that uses Django in server-side. Im trying to do that: import uuid from base64 import b64decode from django.core.files.base import ContentFile @staticmethod def add_photo(user, person, image_base64): photo = DatabasePersonPhoto() photo.user = user photo.person = person image_data = b64decode(image_base64) image_name = str(uuid.uuid4())+".jpg" photo.image = ContentFile(image_data, image_name) photo.save() return photo This is my Base64 String: data:image/jpeg;base64,/9j

Save Base64 String into Django ImageField

巧了我就是萌 提交于 2021-02-07 12:15:30
问题 Im doing an application that uses Django in server-side. Im trying to do that: import uuid from base64 import b64decode from django.core.files.base import ContentFile @staticmethod def add_photo(user, person, image_base64): photo = DatabasePersonPhoto() photo.user = user photo.person = person image_data = b64decode(image_base64) image_name = str(uuid.uuid4())+".jpg" photo.image = ContentFile(image_data, image_name) photo.save() return photo This is my Base64 String: data:image/jpeg;base64,/9j

Using existing field values in django update query

社会主义新天地 提交于 2021-02-07 12:14:29
问题 I want to update a bunch of rows in a table to set the id = self.id. How would I do the below? from metadataorder.tasks.models import Task tasks = Task.objects.filter(task_definition__cascades=False) .update(shared_task_id=self.id) The equivalent SQL would be: update tasks_task t join tasks_taskdefinition d on t.task_definition_id = d.id set t.shared_task_id = t.id where d.cascades = 0 回答1: You can do this using an F expression: from django.db.models import F tasks = Task.objects.filter(task

Send an email if to a Django User if their active status is changed

时间秒杀一切 提交于 2021-02-07 10:46:15
问题 I've built a site that requires membership for some portions. It's a club website so to be a member on the website, you have to be a member in real life. The plan is to have somebody from the Club check for new members (I'll probably have the system send them an email when a user signs up) and then the admin checks the active checkbox under the user's record in the Django admin and saves the user. The problem I'm trying to overcome is we need new, valid users to be notified as to when they

Django Python : adding custom permissions to specific users

百般思念 提交于 2021-02-07 09:57:40
问题 Models.py class Meta: permissions = ( ("can_add_data","can add a new data"), ) This is the custom permission I've created in Models.py and I've also created these users. Users in Django Admin ie., http://localhost:8000/admin How do I give permission to specific users so that I can use @permission_required('myapp.can_add_data') in views.py and also where do I write the snippet? (in which file) I'm a beginner at this so if there are any mistakes please let me know. 回答1: You can assign

Django Python : adding custom permissions to specific users

旧时模样 提交于 2021-02-07 09:57:26
问题 Models.py class Meta: permissions = ( ("can_add_data","can add a new data"), ) This is the custom permission I've created in Models.py and I've also created these users. Users in Django Admin ie., http://localhost:8000/admin How do I give permission to specific users so that I can use @permission_required('myapp.can_add_data') in views.py and also where do I write the snippet? (in which file) I'm a beginner at this so if there are any mistakes please let me know. 回答1: You can assign

how to fix the 'AnonymousUser' object has no attribute 'profile' error?

╄→гoц情女王★ 提交于 2021-02-07 06:48:43
问题 I'm writing a chat app for a hypothetical social network but when I try to open the chat page I give the following error 'AnonymousUser' object has no attribute 'profile' error . I think there may be problem in the models file but I can't really figure out how to fix it and I'm really confused now!? can anyone give any suggestions?? parts of the chat views.py def index(request): if request.method == 'POST': print request.POST request.user.profile.is_chat_user=True logged_users = [] if request

include() got an unexpected keyword argument 'app_name'

家住魔仙堡 提交于 2021-02-07 06:10:27
问题 i am making a blog application for my website with django-2.0 when i run server i see the following error File "C:\Users\User\Desktop\djite\djite\djite\urls.py", line 7, in <module> url(r'^blog/', include('blog.urls', namespace='blog', app_name='blog')), TypeError: include() got an unexpected keyword argument 'app_name' here is my main urls.py from django.contrib import admin from django.conf.urls import url,include urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^blog/', include(

include() got an unexpected keyword argument 'app_name'

五迷三道 提交于 2021-02-07 06:06:43
问题 i am making a blog application for my website with django-2.0 when i run server i see the following error File "C:\Users\User\Desktop\djite\djite\djite\urls.py", line 7, in <module> url(r'^blog/', include('blog.urls', namespace='blog', app_name='blog')), TypeError: include() got an unexpected keyword argument 'app_name' here is my main urls.py from django.contrib import admin from django.conf.urls import url,include urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^blog/', include(

Django/Python update field values (during model save)

爷,独闯天下 提交于 2021-02-07 03:37:21
问题 I am trying to capitalize a number of fields in my django models, when they are saved. Looking at this question, which had this answer: class Blog(models.Model): name = models.CharField(max_length=100) def save(self): self.name = self.name.title() super(Blog, self).save() This works fine, however, in each save, this requires some extra typing, if I want to repeat this multiple times. So I would like to create a function that takes fields as an input and re-saves them as uppercase, during the