Beispiel #1
0
def testdb(request):
    if request.method == "POST":
        print("Post")
    elif request.method == "GET":
        print("Get")
        aa = request.GET["aa"]
        question = Question(question_text=aa, pub_date=datetime.now())
        question.save()
    print(aa + "_id is " + str(question.id))

    # get
    question = Question.objects.get(id=1)

    # update
    question.question_text = datetime.now().strftime("%H:%M:%S")
    question.save()

    # delete
    question = Question(question_text="10086", pub_date=datetime.now())
    question.save()
    print("10086_id is " + str(question.id))
    question.delete()

    resp = {'errorcode': 100, 'detail': '数据添加成功!'}
    return HttpResponse(json.dumps(resp), content_type="application/json")
Beispiel #2
0
    def handle(self, *args, **options):
        # ...
        if options['delete']:
            Poll.delete()
        for poll_id in options['poll_id']:
            try:
                poll = Poll.objects.get(pk=poll_id)
            except Poll.DoesNotExist:
                raise CommandError('Poll "%s" does not exist' % poll_id)

            poll.opened = False
            poll.save()

            self.stdout.write(
                self.style.SUCCESS('Successfully closed poll "%s"' % poll_id))
Beispiel #3
0
from polls.models import Question, Choice
from django.utils import timezone
import datetime


q1 = Question(question_text="tt",pub_date=timezone.now())
q1.save()
q1.choice_set.create(choice_text='fdsaf',votes=1)
q1.was_published_recently()
q1.delete()
len(Question.objects.all())

q = Question.objects.get(pk=1)

q.choice_set.all()
c=q.choice_set.create(choice_text = 'just fdsa', votes=0)
c.question
q.choice_set.all()

Choice.objects.all()
kk=len(Choice.objects.all())-1

print "key number", c.id,kk
c1  = q.choice_set.get(pk=kk)
print c1
c1.delete()

for sc in Choice.objects.all():
    print sc.choice_text,sc.id

Beispiel #4
0
<Question: Jaka jest twoja ulubiona pora roku?>
>>> q2.id
>>> q2.id is None
True
>>> q2.save()
>>> q2.id
4
>>> Question.objects.filter(question_text="Nie istnieje").update(pub_date=xyz)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'xyz' is not defined
>>> Question.objects.filter(question_text="Nie istnieje").delete()
(0, {})
>>> q2
<Question: Jaka jest twoja ulubiona pora roku?>
>>> q2.delete()
(1, {'polls.Question': 1})
>>> q2
<Question: Jaka jest twoja ulubiona pora roku?>
>>> Choice.objects.all()
<QuerySet [<Choice: Tak>, <Choice: Nie>, <Choice: 12.00>, <Choice: 12.30>, <Choice: 13.00>, <Choice: Biały>, <Choice: Czarny>, <Choice: Morski>]>
>>>
>>>
>>> Question.objects.all()
<QuerySet [<Question: Czy na Brackiej pada deszcz?>, <Question: O której powinna być przerwa?>, <Question: Jaki jest twój ulubiony kolor?>]>
>>> q1.choice_set.all()
<QuerySet [<Choice: Tak>, <Choice: Nie>]>
>>> q1.choice_set.order_by('choice_text')
<QuerySet [<Choice: Nie>, <Choice: Tak>]>
>>> q1.choice_set.order_by('-choice_text')
<QuerySet [<Choice: Tak>, <Choice: Nie>]>
choicesFor_q = q.choice_set.all()
print "  ", choicesFor_q
choicesCount = q.choice_set.count()
print "  ", choicesCount

countAfterCreateQ = Question.objects.count()
print "  ", countAfterCreateQ

#Delete the Question (q)

qObjs = Question.objects.all()
myListPrint(qObjs)
qCount = Question.objects.count()
print "  ", qCount

q.delete()
print "   Just Deleted the Question"

qObjs = Question.objects.all()
myListPrint(qObjs)
qCount = Question.objects.count()
print "  ", qCount
print "   "

print "Dispay a Question and if it exists display its Choices along with additional info"
listID = [1, 5]
for idVal in listID:
    print "idVal= ", idVal
    try:
        q = Question.objects.get(id=idVal)
    except Question.DoesNotExist: