Using __str__() method in Django on Python 2

2024/10/2 22:23:52

I am Learning Django using the Django project tutorial. Since I use python 2.7 I am unable to implement the following in python 2.7:

from django.db import modelsclass Question(models.Model):
# ...def __str__(self):              # __unicode__ on Python 2return self.question_textclass Choice(models.Model):
# ...def __str__(self):              # __unicode__ on Python 2return self.choice_text
Answer

To keep the code compatible between py2 and py3, a better way is to use the decorator python_2_unicode_compatible. This way you can keep the str method:

from django.db import models
from django.utils.encoding import python_2_unicode_compatible@python_2_unicode_compatible
class Question(models.Model):
# ...def __str__(self):              # __unicode__ on Python 2return self.question_text@python_2_unicode_compatible
class Choice(models.Model):
# ...def __str__(self):              # __unicode__ on Python 2return self.choice_text

Reference: https://docs.djangoproject.com/en/1.8/topics/python3/#str-and-unicode-methods

Django provides a simple way to define str() and unicode() methods that work on Python 2 and 3: you must define a str() method returning text and to apply the python_2_unicode_compatible() decorator.

...

This technique is the best match for Django’s porting philosophy.

https://en.xdnf.cn/q/70803.html

Related Q&A

Losing merged cells border while editing Excel file with openpyxl

I have two sheets in an Excel file and the first one is a cover sheet which I dont need to edit. There are a few merged cells in the cover sheet, and when I edit the file using openpyxl, without even t…

Reset CollectorRegistry of Prometheus lib after each unit test

I have a class A that initializes a Counter in its initfrom prometheus_client import Counter class A:def __init__(self):self.my_counter = Counter(an_awesome_counter)def method_1(self):return 1def metho…

Why does Django ORM allow me to omit parameters for NOT NULL fields when creating an object?

Stupid question time. I seem to be able to create an object for a Django model even though I omit a column that was defined as NOT NULL and I dont understand why. Heres my model:class Movie(models.Mo…

Where should i do the django validations for objects and fields?

Im creating a django application which uses both the Django Rest Framework and the plain django-views as entrypoint for users.I want to do validation both independant fields of my models, and on object…

Why does bytes.fromhex() treat some hex values strangely?

Im trying to use the socket library in Python to send bytes of two hex digits to a piece of hardware programmed to accept them. To create the bytes from a user-entered string of hex digits, Im trying …

Extracting beats out of MP3 music with Python

What kind of solutions are there to analyze beats out of MP3 music in Python? The purpose of this would be to use rhythm information to time the keyframes of generated animation, export animation as v…

Switch to popup in python using selenium

How do I switch to a popup window in the below selenium program. Ive looked up for all possible solutions but havent been able to get my head around them. Please help!!from selenium import webdriver fr…

Segmentation fault while redirecting sys.stdout to Tkinter.Text widget

Im in the process of building a GUI-based application with Python/Tkinter that builds on top of the existing Python bdb module. In this application, I want to silence all stdout/stderr from the consol…

Why do pandas and dask perform better when importing from CSV compared to HDF5?

I am working with a system that currently operates with large (>5GB) .csv files. To increase performance, I am testing (A) different methods to create dataframes from disk (pandas VS dask) as well a…

is there any pool for ThreadingMixIn and ForkingMixIn for SocketServer?

I was trying to make an http proxy using BaseHttpServer which is based on SocketServer which got 2 asynchronous Mixins (ThreadingMixIn and ForkingMixIn)the problem with those two that they work on each…