How to save unicode string in admin site django? - django

I want to change data in admin site of django but when I save it, it will turn to something like 'C?a 4'. '?' is the unicode word. I have change data in database to utf8mb4 and if I change data directly in mysql, it will display properly in admin site. However, it can not display like i want if I change data in admin site of django. I'm using Python 3. This is my model
class Camera(models.Model):
camera_id = models.AutoField(primary_key=True)
camera_name = models.CharField(max_length=45, blank=True, null=True)
stream_url = models.CharField(max_length=2083, blank=True, null=True)
class Meta:
managed = True
db_table = 'camera'
def __str__(self):
return self.camera_name

Related

Django, getting data from another user (schema) in an Oracle 19 DB

I am using Django 4.1, Oracle 19 with the python package oracledb.
I am logged in as user djangousr and the schema I am trying to get the data from is "user123"
I am able to retrieve data in a file outside of Django with the same connection information.
But in Django, I keep getting the same error.
I hope you have a solution for me as I was not able to find anything elsewhere.
Thank you.
ORA-00942: table or view does not exist
Below is the SQL from the debug screen that I am able to run fine in SQL developer.
('SELECT "USER"."USER_ID" FROM "user123.USER_TABLE"')
I will also provide the model and the view:
class SecurityUserData(models.Model):
user_id = models.IntegerField(primary_key=True)
user_name = models.CharField(max_length=100)
user_password = models.CharField(max_length=100)
user_first_name = models.CharField(max_length=30, blank=True, null=True)
user_last_name = models.CharField(max_length=30, blank=True, null=True)
class Meta:
managed = False
db_table = 'SEC_USER'
And the view:
def display_user_data(request):
user_data = SecurityUserData.user123.all()
return render(request, 'all.html', {'user_data': user_data})

Django Mezzanine - Simple custom Admin Form

I need to add job offers section to my company site (as a intro to django for me). The problem is that when i inherit my model from mezzanine's Page model it adds to admins create form all bunch of field which i dont need (like publish dates, draft field, comment field etc). I want to make create/edit job offers form as simple as possible.
I tried to inherit it from basic models.Model but it throws an error ...
Unknown column 'project_joboffer.id' in 'field list'"
I tried to customize Admin Form but im still getting error above.
models.py
class JobOffer(models.Model):
title = models.CharField(max_length=255, null=False, blank=False)
place = models.CharField(max_length=255, null=True, blank=True)
date = models.DateTimeField(auto_now_add=True)
content = models.TextField(blank=False,null=False)
published = models.BooleanField(default=True)
deleted = models.NullBooleanField()
forms.py
from django import forms
from ckeditor.widgets import CKEditorWidget
from models import JobOffer
class JobOfferForm(forms.ModelForm):
title = forms.CharField(max_length=255, required=True)
place = forms.CharField(max_length=255, required=False)
content = forms.CharField(required=True , widget=CKEditorWidget())
published = forms.BooleanField(initial=True)
deleted = forms.NullBooleanField()
# class Meta:
# model = JobOffer
admin.py
class JobOfferAdmin(admin.ModelAdmin):
form = JobOfferForm
admin.site.register(JobOffer, JobOfferAdmin)
OK, i fixed it. Migrations creating and deleting wasnt enough. I dont know why but this time i had to also delete entry in django_migrations table.

Django admin site gives error on Djangae

I have a Django app that works perfectly on google app engine, using the datastore via djangae. However, the admin site throws an error:
NotSupportedError at /admin/auth/user/5629499534213120/change/
Cross-join where filters are not supported on the Datastore
This error only occurs when trying to edit the default Django user model. Not sure why this is happening.
I have used the default Django user model. (this is an app dealing with donations for a nonprofit)
models.py:
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class FoodSplashUser(models.Model):
base_user = models.OneToOneField(User, on_delete=models.CASCADE)
address = models.TextField(null=True)
city = models.TextField(null=True)
state = models.CharField(max_length=4, null=True)
zip = models.CharField(max_length=10, null=True)
def __str__(self):
return str(self.base_user.username)
class Organization(models.Model):
base_user = models.OneToOneField(User, on_delete=models.CASCADE)
address = models.TextField(null=True)
city = models.TextField(null=True)
state = models.CharField(max_length=4, null=True)
zip = models.CharField(max_length=10, null=True)
description = models.TextField(null=True)
image_url = models.URLField(null=True)
def __str__(self):
return str(self.base_user.username)
class DonationRequest(models.Model):
organization = models.ForeignKey(Organization, on_delete=models.CASCADE)
timestamp = models.DateTimeField(auto_now=True)
request_type = models.TextField(null=True)
description = models.TextField(null=True)
def __str__(self):
return str(self.organization.base_user.username) + " " + self.request_type
class DonationPromise(models.Model):
user = models.ForeignKey(FoodSplashUser, on_delete=models.CASCADE)
donation_request = models.ForeignKey(DonationRequest, on_delete=models.CASCADE)
timestamp = models.DateTimeField(auto_now=True)
verified = models.BooleanField(default=False)
def __str__(self):
return str(self.user.base_user.username) + " " + str(self.donation_request)
This app goes with the default Django admin interface, but I decided to make the classes below for easy editing later.
admin.py:
from django.contrib import admin
from . import models
# Register your models here.
class FoodSplashUserAdmin(admin.ModelAdmin):
pass
class OrganizationAdmin(admin.ModelAdmin):
pass
class DonationRequestAdmin(admin.ModelAdmin):
pass
class DonationPromiseAdmin(admin.ModelAdmin):
pass
admin.site.register(models.FoodSplashUser, FoodSplashUserAdmin)
admin.site.register(models.Organization, OrganizationAdmin)
admin.site.register(models.DonationRequest, DonationPromiseAdmin)
admin.site.register(models.DonationPromise, DonationPromiseAdmin)
This may be a separate error, but :
admin.site.register(models.DonationRequest, DonationPromiseAdmin)
admin.site.register(models.DonationPromise, DonationPromiseAdmin)
Shouldn't that first one be: DonationRequestAdmin?
NotSupportedError indicates that your code performs an action that is not possible with App Engine Datastore. Not all the Django ORM features can be used in a non-relational database which Datastore is. You are trying to create an entity that has some relations, which causes the error. Probably it is a good idea to use Gauth for authentication and user management, as described in the Djangae docs.

Edit foreign key value in Django admin

I have model an Django model as follows:
class BusinessUnit(models.Model):
name = models.CharField(max_length=255)
address = models.ForeignKey(to=Address)
vat_number = models.CharField(max_length=50)
telephone = models.CharField(max_length=50)
email = models.CharField(max_length=50)
manager = models.ForeignKey(to=User, null=True)
def __unicode__(self):
return self.name
How can I change address directly from BusinessUnit in Django admin?
Now I can only select the address, but I cant change it. I need to go to Address model and change it there, but I want to be able to change it directly in BusinessUnit model.
Any idea how can I do that?

Why is a Django model taking so long to load in admin?

I have a fairly simple Django set up for a forum, and one of the most basic models is this, for each thread:
class Post(models.Model):
created = models.DateTimeField(auto_now_add=True)
last_reply = models.DateTimeField(auto_now_add=True, blank=True, null=True)
username = models.ForeignKey(User, related_name="forumuser")
fixed = models.BooleanField(_("Sticky"), default=False)
closed = models.BooleanField(default=False)
markdown_enabled = models.BooleanField(default=False)
reply_count = models.IntegerField(default=0)
title = models.CharField(_("Title Post"), max_length=255)
content = models.TextField(_("Content"), blank=False)
rating = models.IntegerField(default=0)
followers = models.IntegerField(default=0)
ip_address = models.CharField(max_length=255)
def __unicode__(self):
return self.title
def get_absolute_url(self):
return "/post/%s/" % self.id
Then we have some replies:
class PostReply(models.Model):
user = models.ForeignKey(User, related_name='replyuser')
post = models.ForeignKey(Post, related_name='replypost')
created = models.DateTimeField(auto_now_add=True)
content = models.TextField()
ip_address = models.CharField(max_length=255)
quoted_post = models.ForeignKey('self', related_name='quotedreply', blank=True, null=True)
rating = models.IntegerField(default=0)
reply_order = models.IntegerField(default=1)
Now, currently there just over 1600 users, 6000 Posts, and 330,000 PostReply objects in the db for this setup. When I run this SQL query:
SELECT * FROM `forum_post` LIMIT 10000
I see that Query took 0.0241 sec which is fine. When I browse to the Django admin section of my site, pulling up an individual Post is rapid, as is the paginated list of Posts.
However, if I try and pull up an individual PostReply, it takes around 2-3 minutes to load.
Obviously each PostReply admin page will have a dropdown list of all the Posts in it, but can anyone tell me why this or anything else would cause such a dramatically slow query? It's worth noting that the forum itself is pretty fast.
Also, if it is something to do with that dropdown list, has anyone got any suggestions for making that more usable?
Try to add all foreign keys in raw_id_fields in admin
class PostReplyAdmin(ModelAdmin):
raw_id_fields = ['user', 'post', 'quoted_post']
This will decrease page's load time in change view. The problem is that django loads ForeignModel.objects.all() for each foreign key's dropdowns.
Another way is to add foreign keys in autocomplete_fields (docs) in admin
class PostReplyAdmin(ModelAdmin):
autocomplete_fields = ['user', 'post', 'quoted_post']
As pointed by #Andrey Nelubin the problem for me was indeed in the page loading all related models for each foreign key's dropdown. However, with autocomplete_fields selects are turned into autocomplete inputs (see figure below), which load options asynchronously.