django-models

How to save FileField with None in Django?

坚强是说给别人听的谎言 提交于 2021-01-29 07:16:26
问题 I have model Profile with Avatar field use FileField. class Profile(models.Model): avatar = models.FileField("Uploaded avatar of profile", storage=OverwriteStorage(), upload_to=avatar_photo_upload, null=True, blank=True) I write function to remove avatar, which set avatar = None def remove_avatar(self, request): profile = Profile.objects.get(user=request.user) profile.avatar = None profile.avatar.save() return Response({ 'status': 'ok', 'message': 'Avatar successfully removed' }, status

Django + HTML: Why does my if condition in my template fail even thought I havent submitted interest?

心不动则不痛 提交于 2021-01-29 06:11:59
问题 Django + HTML: Why does my if condition in my template fail even thought I havent submitted interest? As you can see from my views.py i have already indicated that if you have submitted interest, you will get the message that you have submitted the interest, otherwise the button 'submit interest' will be shown. Why does it not work then? views.py def detail_blog_view(request, slug): context = {} #need to import a package get_object_or_404. return object or throw 404 blog_post = get_object_or

Can't login to django admin account after creating super user

眉间皱痕 提交于 2021-01-29 06:06:13
问题 I created a project in django and the first thing I want to do is to create a superuser for admin account and then proceed with the django project but the problem is after creating a superuser account using python manage.py createsuperuser and filling out the information the superuser gets created it says Superuser created successfully. But when I try to login with these credentials it says Please enter the correct username and password for a staff account. Note that both fields may be case

Django custom manager filter query set (by parameter)

不问归期 提交于 2021-01-29 04:38:39
问题 I would like my models to automatically filter by current user. I did this by defining: class UserFilterManager(models.Manager): def get_queryset(self): return super(UserFilterManager, self).get_queryset().filter( owner=get_current_user() ) where get_current_user() is a middleware which extracts the current user from the request passed to Django. However, I need to use the models from Celery which does not go through the middleware. In these cases MyModel.objects.all() needs to become MyModel

Django custom manager filter query set (by parameter)

折月煮酒 提交于 2021-01-29 04:36:57
问题 I would like my models to automatically filter by current user. I did this by defining: class UserFilterManager(models.Manager): def get_queryset(self): return super(UserFilterManager, self).get_queryset().filter( owner=get_current_user() ) where get_current_user() is a middleware which extracts the current user from the request passed to Django. However, I need to use the models from Celery which does not go through the middleware. In these cases MyModel.objects.all() needs to become MyModel

Django SQL Logging as UTF-8 : avoiding UnicodeDecodeError while settings.DEBUG=True

牧云@^-^@ 提交于 2021-01-29 03:15:14
问题 For a long time, I've had to struggle with a "UnicodeDecodeError:ascii codec can't decode..." error while save()-ing a model, until I "accidentally" set settings.DEBUG=False. I realized then that the Unicode exception was not from my data nor from my code, but from Django's SQL logging, which is apparently in ascii. Question is, is there an easy way to convert the logging from ascii to utf-8, so that I can still avail of the logging feature without dealing with the exception? Many thanks! =)

Does deleting migrations and database in dev, cause issues when pushing to production?

梦想的初衷 提交于 2021-01-28 21:12:58
问题 I had a conflict in my development database, so to make things easier for me I deleted all my migrations and dropped database and created a new one. Now in development every thing works correctly: find . -path "*/migrations/*.py" -not -name "__init__.py" -delete find . -path "*/migrations/*.pyc" -delete dropdb 'mydb' createdb 'mydb' python manage.py makemigrations python manage.py migrate I then saved a git commit and pushed to production and got the following error: django.db.utils

what wrong with my models.py

走远了吗. 提交于 2021-01-28 18:59:21
问题 I am using Django 1.4 with PostgreSQL 9.1.4 what wrong with my models.py class UserProfile(models.Model): ROLES_TYPE = ( ('Employee', 'Employee'), ('Employor', 'Employor'), ) user = models.OneToOneField(User, related_name='Profile') role = models.CharField(max_length=15, choices=ROLES_TYPE) class Student(models.Model): id = models.AutoField(db_column='studentid', primary_key=True) username = models.OneToOneField(UserProfile, related_name='Employee') first_name = models.CharField(db_column=

Django Signal post_save()

喜你入骨 提交于 2021-01-28 17:46:39
问题 Once the system detects that you already invited 2 users, your profile will automatically save in graduate list please see this picture, as example: this is the table of graduate list. as you can see in the picture, Joe Dio has already invited 2 users ( miro and justin ), (you can see it in the Sponsor user ) I hope you can help me to figure it out using django signal, thanks in advance :) i wish the example I made had you understood what i wanted to get this is my models.py class User(models

How to delete the first name and last name columns in django user model?

落花浮王杯 提交于 2021-01-28 10:51:34
问题 I created a custom user model just like the docs said, when I ran the makemigrations I went to the folder of migrations and deleted the first name and last name columns inside the 0001_initial.py and then I ran the migrate command, but when I ran the createsuperuser command it throwed an error in the terminal that django.db.utils.OperationalError: no such column: myapp_usermodel.first_name How can I fix this? I want to delete the first name and last name columns because I'm not using them