Django从数据库中获取数据

时间:2016-01-12 18:38:45

标签: python django

我需要一个功能:

def GetData(self, column):
   query = UserData.objects.get(Username="Stack")
   return query.column

并使用此:

ClassName.GetData(request, 'Email')

不幸的是,当然,它不会起作用。如何使其按预期方式工作?

1 个答案:

答案 0 :(得分:0)

def get_data(self, column):
    query = UserData.objects.get(Username="Stack")
    return getattr(query, column)

要打电话:

class_name.get_data('email')

getattr python doc

我更改了您的函数名称,因为使用GetData作为函数名称是一个坏主意。您应该关注函数和类的pep8 convention

此外,我不知道ClassName是什么,但如果您的第一个参数是self,则应使用object.function() object self来调用#include "SFML\Graphics.hpp" #include <iostream> #include <string> using namespace std; int main(){ //initialize window object sf::RenderWindow window(sf::VideoMode(800, 600), "SFML"); sf::CircleShape polygon(50,4); polygon.rotate(45); polygon.move(sf::Vector2f(200,200)); //as long as we haven't closed the window while (window.isOpen()) { sf::Event event; //check for events while (window.pollEvent(event)) { switch (event.type) { case sf::Event::Closed: //check for CLOSED event window.close(); break; } polygon.rotate(0.4); } window.clear(); window.draw(polygon); window.display(); } } {1}}。

相关问题