首页 > 其他分享 >What is doing __str__ function in Django?

What is doing __str__ function in Django?

时间:2023-05-25 17:15:22浏览次数:52  
标签:__ function What sheet topics share data se

def str(self): is a python method which is called when we use print/str to convert object into a string. It is predefined , however can be customised. Will see step by step.Suppose below is our code.

class topics():
    def __init__(self,topics):
        print "inside init"
        self.topics=topics
my_top = topics("News")
print(my_top)

Output:

inside init
<__main__.topics instance at 0x0000000006483AC8>

So while printing we got reference to the object. Now consider below code.

class topics():
    def __init__(self,topics):
        print "inside init"
        self.topics=topics

    def __str__(self):
        print "Inside __str__"
        return "My topics is " + self.topics
my_top = topics("News")
print(my_top)

Output:

inside init
Inside __str__
My topics is News

So, here instead of object we are printing the object. As we can see we can customize the output as well. Now, whats the importance of it in a django models.py file?

When we use it in models.py file, go to admin interface, it creates object as "News", otherwise entry will be shown as main.topics instance at 0x0000000006483AC8 which won't look that much user friendly.

enter image description here

 

标签:__,function,What,sheet,topics,share,data,se
From: https://www.cnblogs.com/weifeng1463/p/17431872.html

相关文章

  • PMP 学习笔记(三)
    项目范围:为交付具有规定特性与功能的产品、服务或成果而必须完成的工作。项目范围有时也包括产品范围 预测型项目在每次迭代中,都会重复开展三个过程:收集需求、定义范围、创建WBS。 敏捷型项目中每次迭代中,都会重复开张两个过程:确认范围、控制范围。 对于需求不断变化、风险大或不......
  • 支持复制粘贴word公式的Web编辑器
    ​ 这种方法是servlet,编写好在web.xml里配置servlet-class和servlet-mapping即可使用后台(服务端)java服务代码:(上传至ROOT/lqxcPics文件夹下)<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@     page contentType="text/html;cha......
  • Spring Web Body 转化常见错误
    案例1:Noconverterfoundforreturnvalueoftype在直接用SpringMVC而非SpringBoot来编写Web程序时,我们基本都会遇到“Noconverterfoundforreturnvalueoftype”这种错误。实际上,我们编写的代码都非常简单,例如下面这段代码:(https://www.java567.com,搜"spring"......
  • 从输入URL到页面加载发生了什么?
    1、首先进行DNS域名解析,目的是找到对应的ip地址DNS解析过程:首先搜索浏览器自身的DNS缓存-----》没有找到则去本机的host文件中找-------》发起DNS递归查询,先查本地域名服务器———》根域名服务器———》com顶级域名服务器直到最后本地域名服务器得到google的IP地址并把它缓存......
  • idea修改idea64.exe.vmoptions导致打不开问题
    问题原因是在idea中改了idea64.exe.vmoptions配置,导致idea打不开。网上帖子很多说是C盘appdata里面的缓存idea64.exe.vmoptions文件删除或者更改成跟安装目录一样的就行了。其实得看你的idea是不是用了激活工具,没有用激活工具当然按那种方式就可以解决。如果用了激活工具,激活工......
  • Python集合 (set) 的增删改查及 copy()方法
    集合是无序的,不重复的数据集合,它里面的元素是可哈希的(不可变类型),但是集合本身是不可哈希(所以集合做不了字典的键)的。以下是集合最重要的两点:1、去重,把一个列表变成集合,就自动去重了。2、关系测试,测试两组数据之前的交集、差集、并集等关系。一、集合的创建set1=set({1,2......
  • 相对路径绝对路径
    目录文件夹是项目的目录。根目录是打开目录文件夹的第一层相对路径:比如在一个html中引入图片。它指的就是图片相对于html的位置     注意:绝对路径和相对路径的符号不一样,相对路径的地址用的是 / 绝对路径用的是\ ......
  • AtCoder Regular Contest 146 D >=<
    洛谷传送门AtCoder传送门考虑直接增量构造。把条件拆成形如\(a_u\gex\)时\(a_v\gets\max(a_v,y)\),连边,跑类似一个spfa的东西,就行了。这样一定能构造出一组和最小的解。code//Problem:D->=<//Contest:AtCoder-AtCoderRegularContest146//URL:http......
  • 阶与原根
    仅包含基础知识以及部分感性理解,不包括严谨证明。OI_Wiki。阶与原根阶我们知道\((a,m)=1\)时,\(a^p\)是有循环节\(\varphi(m)\)的,但是我们更好奇最小循环节是多少,于是我们称它为阶\(\delta_m(a)\)。你考虑感性理解这个东西其实就是一个简单环。这里补充一句,当\((a,m)\no......
  • ubuntu安装docker
    官网安装地址:InstallDockerEngineonUbuntuubuntu清华大学云源地址:ubuntu|镜像站使用帮助|清华大学开源软件镜像站|TsinghuaOpenSourceMirrorubuntu卸载1.卸载dockersudoapt-getremovedockerdocker-enginedocker.iocontainerdruncubuntu安装1.更新......