def test_new_poll(self):

        #creating a poll object
        poll = Question()
        poll.question_text = "Shall we begin?"
        poll.pub_date = timezone.now()
        poll.save()
        self.assertEquals(poll.question_text, "Shall we begin?")
Пример #2
0
def get_question(request):
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = QuestionForm(request.POST)
        # check whether it's valid:
        if form.is_valid():
            # process the data in form.cleaned_data as required
            new_question = Question()
            new_question.body = Gymkhana_body.objects.get(
                nameofbody=form.cleaned_data['body'])
            new_question.question_text = form.cleaned_data['question']
            new_question.pub_date = timezone.now()
            new_question.save()

            choice1 = Choice()
            choice1.question = new_question
            choice1.choice_text = form.cleaned_data['choice1']
            choice1.votes = 0
            choice1.save()

            if form.cleaned_data['choice2']:
                choice2 = Choice()
                choice2.question = new_question
                choice2.choice_text = form.cleaned_data['choice2']
                choice2.votes = 0
                choice2.save()

            if form.cleaned_data['choice3']:
                choice3 = Choice()
                choice3.question = new_question
                choice3.choice_text = form.cleaned_data['choice3']
                choice3.votes = 0
                choice3.save()

            if form.cleaned_data['choice4']:
                choice4 = Choice()
                choice4.question = new_question
                choice4.choice_text = form.cleaned_data['choice4']
                choice4.votes = 0
                choice4.save()

            # redirect to a new URL:
            messages.add_message(request, messages.SUCCESS,
                                 'Thanks for your suggestion!')
            return HttpResponseRedirect('/')

    # if a GET (or any other method) we'll create a blank form
    else:
        form = QuestionForm()

    return render(request, 'organisation/addquestion.html', {'form': form})
Пример #3
0
    def test_saving_and_retrieving_items(self):
        first_question = Question()
        first_question.question_text = "What's new?"
        first_question.pub_date = timezone.now()
        first_question.save()

        saved_items = Question.objects.all()
        self.assertEqual(saved_items.count(), 1)

        second_question = Question()
        second_question.question_text = "What's up?"
        second_question.pub_date = timezone.now()
        second_question.save()

        saved_items = Question.objects.all()
        self.assertEqual(saved_items.count(), 2)

        first_saved_item = saved_items[0]
        second_saved_item = saved_items[1]
        self.assertEqual(first_saved_item.question_text, "What's new?")
        self.assertEqual(second_saved_item.question_text, "What's up?")
        self.assertEqual(second_saved_item.pub_date, second_question.pub_date)
Пример #4
0
    def test_question_has_chice(self):
        question = Question()
        question.question_text = "has choice"
        question.pub_date = timezone.now()
        question.save()

        question.choice_set.create(choice_text='Not much', votes=0)
        question.choice_set.create(choice_text='The sky', votes=0)

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

        self.assertEqual(saved_item.choice_set.count(), 2)
        self.assertEqual(saved_item.choice_set.first().choice_text, 'Not much')
Пример #5
0
def index(request):

    q = get_object_or_404(Question, id=1)


    try:
        q = Question.objects.get(id=2)
    except Question.DoesNotExist:
        q = Question()
        q.question_text = ""
        q.save()

    return HttpResponse("Hello, world. New Resposne ")
Пример #6
0
    def post(self, request):
        question = Question()
        question.question_text = "the question is what?"
        question.pub_date = timezone.now()

        # 序列化方法1
        serializer = QuestionSerializer(question)
        print(serializer.data)
        #反序列化
        stream = io.BytesIO(JSONRenderer().render(serializer.data))
        data = JSONParser().parse(stream)
        print(QuestionSerializer(data).data)

        return Response(serializer.data)
def i_have_a_list_of_polls(step):
    from polls.models import Question, Choice
    from django.utils import timezone

    #create 5 poll question
    for i in range(0, 5):
        question = Question()
        question.question_text = "Question text %s" % i
        question.pub_date = timezone.now()
        question.save()

        # create 5 choice for each question
        for j in range(0, 5):
            choice = Choice()
            choice.choice_text = "Choice text %s" % j
            choice.question = question
            choice.save()
Пример #8
0
    def test_vote_is_added_question(self):
        """
        was_published_recently() returns False for questions whose pub_date
        is in the future.
        """
        question = Question(question_text='测试', pub_date=timezone.now())
        question.save()

        question = Question.objects.get(pk=1)
        text = question.question_text

        question.question_text = 'modify'
        question.save()

        question = Question.objects.get(pk=1)
        newText = question.question_text

        self.assertEqual(newText, 'modify')
Пример #9
0
def seed(request):
    """Seeds the database with sample polls."""
    samples_path = path.join(path.dirname(__file__), 'samples.json')
    with open(samples_path, 'r') as samples_file:
        samples_polls = json.load(samples_file)

    for sample_poll in samples_polls:
        poll = Question()
        poll.question_text = sample_poll['text']
        poll.pub_date = timezone.now()
        poll.save()

        for sample_choice in sample_poll['choices']:
            choice = Choice()
            choice.question = poll
            choice.choice_text = sample_choice
            choice.votes = 0
            choice.save()

    return HttpResponseRedirect(reverse('polls:question'))
Пример #10
0
    def test_saving_and_retrieving_questions(self):
        tz = timezone.get_default_timezone()
        question = Question()
        question.question_text = 'Who is the most famous actress?'
        question.pub_date = datetime.datetime(
            2012, 3, 31, 11, 30, 5, 182371, tzinfo=tz
        )
        question.save()

        saved_questions = Question.objects.all()
        self.assertEqual(saved_questions.count(), 1)
        saved_question = saved_questions[0]
        self.assertIn('Who is the most famous actress?',
            repr(saved_question),
        )
        self.assertEqual(saved_question.question_text,
            'Who is the most famous actress?')
        self.assertEqual(saved_question.pub_date,
            datetime.datetime(2012, 3, 31, 11, 30, 5, 182371, tzinfo=tz))

        self.assertFalse(saved_question.was_published_recently())
Пример #11
0
from django.test import TestCase

# Create your tests here.
from polls.models import Question

que = Question()
que.question_text('什么水果最好吃')
que.pub_date('2018-04-01')
que.save()
Пример #12
0
# Create a new Question.
# Support for time zones is enabled in the default settings file, so
# Django expects a datetime with tzinfo for pub_date. Use timezone.now()
# instead of datetime.datetime.now() and it will do the right thing.
from django.utils import timezone

q = Question(question_text="What's new?", pub_date=timezone.now())

# Save the object into the database. You have to call save() explicitly.
q.save()

# Now it has an ID. Note that this might say "1L" instead of "1", depending
# on which database you're using. That's no biggie; it just means your
# database backend prefers to return integers as Python long integer
# objects.
print(q.id)


# Access model field values via Python attributes.
print(q.question_text)

print(q.pub_date)

# Change values by changing the attributes, then calling save().
q.question_text = "What's up?"
q.save()

# objects.all() displays all the questions in the database.
Question.objects.all()
Пример #13
0
q.save()

# print id
print(q.id)
# 1

# print text of the question
print(q.question_text)
# What's new?

# print the pub_date of the question
print(q.pub_date)
# 2019-03-26 20:40:46.355213+00:00

# change the text of the question
q.question_text = "What's up?"

# update question in db
q.save()

# display all questions in db
Question.objects.all()
# <QuerySet [<Question: Question object(1)>]>

# add the methods to the polls/models (add the __str__() methods)

# display all question in db
Question.objects.all()
# <QuerySet [<Question: What's up?>]>

# get Question element with id 1
Пример #14
0
def import_data():
    # Initial Imports

    # Processing model: polls.models.Question

    from polls.models import Question

    polls_question_1 = Question()
    polls_question_1.question_text = 'How are you?'
    polls_question_1.pub_date = dateutil.parser.parse(
        "2018-06-11T07:57:12.927873+00:00")
    polls_question_1 = importer.save_or_locate(polls_question_1)

    polls_question_2 = Question()
    polls_question_2.question_text = 'How mad are you?'
    polls_question_2.pub_date = dateutil.parser.parse(
        "2018-06-11T07:57:34.837612+00:00")
    polls_question_2 = importer.save_or_locate(polls_question_2)

    polls_question_3 = Question()
    polls_question_3.question_text = 'How wrong are you?'
    polls_question_3.pub_date = dateutil.parser.parse(
        "2018-06-11T07:57:55.751242+00:00")
    polls_question_3 = importer.save_or_locate(polls_question_3)

    polls_question_4 = Question()
    polls_question_4.question_text = 'How happy are you??'
    polls_question_4.pub_date = dateutil.parser.parse(
        "2018-06-11T08:33:45+00:00")
    polls_question_4 = importer.save_or_locate(polls_question_4)

    # Processing model: polls.models.Choice

    from polls.models import Choice

    polls_choice_1 = Choice()
    polls_choice_1.question = polls_question_1
    polls_choice_1.choice_text = 'You are the man!'
    polls_choice_1.votes = 1
    polls_choice_1 = importer.save_or_locate(polls_choice_1)

    polls_choice_2 = Choice()
    polls_choice_2.question = polls_question_1
    polls_choice_2.choice_text = 'You are the best!'
    polls_choice_2.votes = 2
    polls_choice_2 = importer.save_or_locate(polls_choice_2)

    polls_choice_3 = Choice()
    polls_choice_3.question = polls_question_2
    polls_choice_3.choice_text = 'You are the man!'
    polls_choice_3.votes = 4
    polls_choice_3 = importer.save_or_locate(polls_choice_3)

    polls_choice_4 = Choice()
    polls_choice_4.question = polls_question_3
    polls_choice_4.choice_text = 'You are the one!'
    polls_choice_4.votes = 7
    polls_choice_4 = importer.save_or_locate(polls_choice_4)

    polls_choice_5 = Choice()
    polls_choice_5.question = polls_question_2
    polls_choice_5.choice_text = 'You are mad!'
    polls_choice_5.votes = 4
    polls_choice_5 = importer.save_or_locate(polls_choice_5)

    polls_choice_6 = Choice()
    polls_choice_6.question = polls_question_4
    polls_choice_6.choice_text = 'Very Happy'
    polls_choice_6.votes = 2
    polls_choice_6 = importer.save_or_locate(polls_choice_6)

    polls_choice_7 = Choice()
    polls_choice_7.question = polls_question_4
    polls_choice_7.choice_text = 'Damn Happy'
    polls_choice_7.votes = 3
    polls_choice_7 = importer.save_or_locate(polls_choice_7)

    polls_choice_8 = Choice()
    polls_choice_8.question = polls_question_4
    polls_choice_8.choice_text = 'Super duper Happy'
    polls_choice_8.votes = 3
    polls_choice_8 = importer.save_or_locate(polls_choice_8)

    # Processing model: polls.models.mModel

    from polls.models import mModel

    polls_mmodel_1 = mModel()
    polls_mmodel_1.name = 'Ahobaka'
    polls_mmodel_1.gender = 'M'
    polls_mmodel_1.age = 12
    polls_mmodel_1.email = '*****@*****.**'
    polls_mmodel_1.dob = dateutil.parser.parse("2018-06-11")
    polls_mmodel_1.job = 'Student'
    polls_mmodel_1 = importer.save_or_locate(polls_mmodel_1)

    polls_mmodel_2 = mModel()
    polls_mmodel_2.name = 'Ultraman'
    polls_mmodel_2.gender = 'F'
    polls_mmodel_2.age = 23
    polls_mmodel_2.email = '*****@*****.**'
    polls_mmodel_2.dob = dateutil.parser.parse("2018-06-11")
    polls_mmodel_2.job = 'Superhero'
    polls_mmodel_2 = importer.save_or_locate(polls_mmodel_2)

    polls_mmodel_3 = mModel()
    polls_mmodel_3.name = 'Supergirl'
    polls_mmodel_3.gender = 'F'
    polls_mmodel_3.age = 26
    polls_mmodel_3.email = '*****@*****.**'
    polls_mmodel_3.dob = dateutil.parser.parse("2018-06-12")
    polls_mmodel_3.job = 'Superheroin'
    polls_mmodel_3 = importer.save_or_locate(polls_mmodel_3)

    polls_mmodel_4 = mModel()
    polls_mmodel_4.name = 'Batgirl'
    polls_mmodel_4.gender = 'F'
    polls_mmodel_4.age = 21
    polls_mmodel_4.email = '*****@*****.**'
    polls_mmodel_4.dob = dateutil.parser.parse("2018-06-12")
    polls_mmodel_4.job = 'Superheroin'
    polls_mmodel_4 = importer.save_or_locate(polls_mmodel_4)

    polls_mmodel_5 = mModel()
    polls_mmodel_5.name = 'benghenglian'
    polls_mmodel_5.gender = 'M'
    polls_mmodel_5.age = 33
    polls_mmodel_5.email = '*****@*****.**'
    polls_mmodel_5.dob = dateutil.parser.parse("2018-06-12")
    polls_mmodel_5.job = 'BengHengLIanKnight'
    polls_mmodel_5 = importer.save_or_locate(polls_mmodel_5)

    polls_mmodel_6 = mModel()
    polls_mmodel_6.name = 'benghenglian'
    polls_mmodel_6.gender = 'M'
    polls_mmodel_6.age = 33
    polls_mmodel_6.email = '*****@*****.**'
    polls_mmodel_6.dob = dateutil.parser.parse("2018-06-12")
    polls_mmodel_6.job = 'BengHengLIanKnight'
    polls_mmodel_6 = importer.save_or_locate(polls_mmodel_6)

    polls_mmodel_7 = mModel()
    polls_mmodel_7.name = 'Superman'
    polls_mmodel_7.gender = 'm'
    polls_mmodel_7.age = 23
    polls_mmodel_7.email = '*****@*****.**'
    polls_mmodel_7.dob = dateutil.parser.parse("2018-06-12")
    polls_mmodel_7.job = 'Knight'
    polls_mmodel_7 = importer.save_or_locate(polls_mmodel_7)

    polls_mmodel_8 = mModel()
    polls_mmodel_8.name = 'Superman'
    polls_mmodel_8.gender = 'm'
    polls_mmodel_8.age = 23
    polls_mmodel_8.email = '*****@*****.**'
    polls_mmodel_8.dob = dateutil.parser.parse("2018-06-12")
    polls_mmodel_8.job = 'Knight'
    polls_mmodel_8 = importer.save_or_locate(polls_mmodel_8)

    polls_mmodel_9 = mModel()
    polls_mmodel_9.name = 'Superman'
    polls_mmodel_9.gender = 'm'
    polls_mmodel_9.age = 23
    polls_mmodel_9.email = '*****@*****.**'
    polls_mmodel_9.dob = dateutil.parser.parse("2018-06-12")
    polls_mmodel_9.job = 'Knight'
    polls_mmodel_9 = importer.save_or_locate(polls_mmodel_9)

    polls_mmodel_10 = mModel()
    polls_mmodel_10.name = 'Superman'
    polls_mmodel_10.gender = 'm'
    polls_mmodel_10.age = 23
    polls_mmodel_10.email = '*****@*****.**'
    polls_mmodel_10.dob = dateutil.parser.parse("2018-06-12")
    polls_mmodel_10.job = 'Knight'
    polls_mmodel_10 = importer.save_or_locate(polls_mmodel_10)

    polls_mmodel_11 = mModel()
    polls_mmodel_11.name = 'Superman'
    polls_mmodel_11.gender = 'm'
    polls_mmodel_11.age = 23
    polls_mmodel_11.email = '*****@*****.**'
    polls_mmodel_11.dob = dateutil.parser.parse("2018-06-12")
    polls_mmodel_11.job = 'Knight'
    polls_mmodel_11 = importer.save_or_locate(polls_mmodel_11)

    polls_mmodel_12 = mModel()
    polls_mmodel_12.name = 'Superman'
    polls_mmodel_12.gender = 'm'
    polls_mmodel_12.age = 23
    polls_mmodel_12.email = '*****@*****.**'
    polls_mmodel_12.dob = dateutil.parser.parse("2018-06-12")
    polls_mmodel_12.job = 'Knight'
    polls_mmodel_12 = importer.save_or_locate(polls_mmodel_12)

    polls_mmodel_13 = mModel()
    polls_mmodel_13.name = 'Superman'
    polls_mmodel_13.gender = 'm'
    polls_mmodel_13.age = 23
    polls_mmodel_13.email = '*****@*****.**'
    polls_mmodel_13.dob = dateutil.parser.parse("2018-06-12")
    polls_mmodel_13.job = 'Knight'
    polls_mmodel_13 = importer.save_or_locate(polls_mmodel_13)

    polls_mmodel_14 = mModel()
    polls_mmodel_14.name = 'Superman'
    polls_mmodel_14.gender = 'm'
    polls_mmodel_14.age = 23
    polls_mmodel_14.email = '*****@*****.**'
    polls_mmodel_14.dob = dateutil.parser.parse("2018-06-12")
    polls_mmodel_14.job = 'Knight'
    polls_mmodel_14 = importer.save_or_locate(polls_mmodel_14)

    polls_mmodel_15 = mModel()
    polls_mmodel_15.name = 'Superman'
    polls_mmodel_15.gender = 'd'
    polls_mmodel_15.age = 2
    polls_mmodel_15.email = '*****@*****.**'
    polls_mmodel_15.dob = dateutil.parser.parse("2018-06-12")
    polls_mmodel_15.job = 'Knight'
    polls_mmodel_15 = importer.save_or_locate(polls_mmodel_15)

    polls_mmodel_16 = mModel()
    polls_mmodel_16.name = 'Superman'
    polls_mmodel_16.gender = 'd'
    polls_mmodel_16.age = 2
    polls_mmodel_16.email = '*****@*****.**'
    polls_mmodel_16.dob = dateutil.parser.parse("2018-06-12")
    polls_mmodel_16.job = 'Knight'
    polls_mmodel_16 = importer.save_or_locate(polls_mmodel_16)

    polls_mmodel_17 = mModel()
    polls_mmodel_17.name = 'Superman'
    polls_mmodel_17.gender = 'd'
    polls_mmodel_17.age = 2
    polls_mmodel_17.email = '*****@*****.**'
    polls_mmodel_17.dob = dateutil.parser.parse("2018-06-12")
    polls_mmodel_17.job = 'Knight'
    polls_mmodel_17 = importer.save_or_locate(polls_mmodel_17)

    polls_mmodel_18 = mModel()
    polls_mmodel_18.name = 'Superman'
    polls_mmodel_18.gender = 'd'
    polls_mmodel_18.age = 2
    polls_mmodel_18.email = '*****@*****.**'
    polls_mmodel_18.dob = dateutil.parser.parse("2018-06-12")
    polls_mmodel_18.job = 'Knight'
    polls_mmodel_18 = importer.save_or_locate(polls_mmodel_18)

    polls_mmodel_19 = mModel()
    polls_mmodel_19.name = 'Superman'
    polls_mmodel_19.gender = 'd'
    polls_mmodel_19.age = 2
    polls_mmodel_19.email = '*****@*****.**'
    polls_mmodel_19.dob = dateutil.parser.parse("2018-06-12")
    polls_mmodel_19.job = 'Knight'
    polls_mmodel_19 = importer.save_or_locate(polls_mmodel_19)

    polls_mmodel_20 = mModel()
    polls_mmodel_20.name = 'Superman'
    polls_mmodel_20.gender = 'd'
    polls_mmodel_20.age = 2
    polls_mmodel_20.email = '*****@*****.**'
    polls_mmodel_20.dob = dateutil.parser.parse("2018-06-12")
    polls_mmodel_20.job = 'Knight'
    polls_mmodel_20 = importer.save_or_locate(polls_mmodel_20)

    polls_mmodel_21 = mModel()
    polls_mmodel_21.name = 'Superman'
    polls_mmodel_21.gender = 'd'
    polls_mmodel_21.age = 2
    polls_mmodel_21.email = '*****@*****.**'
    polls_mmodel_21.dob = dateutil.parser.parse("2018-06-12")
    polls_mmodel_21.job = 'Knight'
    polls_mmodel_21 = importer.save_or_locate(polls_mmodel_21)

    polls_mmodel_22 = mModel()
    polls_mmodel_22.name = 'Superman'
    polls_mmodel_22.gender = 'd'
    polls_mmodel_22.age = 2
    polls_mmodel_22.email = '*****@*****.**'
    polls_mmodel_22.dob = dateutil.parser.parse("2018-06-12")
    polls_mmodel_22.job = 'Knight'
    polls_mmodel_22 = importer.save_or_locate(polls_mmodel_22)

    polls_mmodel_23 = mModel()
    polls_mmodel_23.name = 'Superman'
    polls_mmodel_23.gender = 'c'
    polls_mmodel_23.age = 2
    polls_mmodel_23.email = '*****@*****.**'
    polls_mmodel_23.dob = dateutil.parser.parse("2018-06-12")
    polls_mmodel_23.job = 'Knight'
    polls_mmodel_23 = importer.save_or_locate(polls_mmodel_23)

    polls_mmodel_24 = mModel()
    polls_mmodel_24.name = 'Superman'
    polls_mmodel_24.gender = 'c'
    polls_mmodel_24.age = 2
    polls_mmodel_24.email = '*****@*****.**'
    polls_mmodel_24.dob = dateutil.parser.parse("2018-06-12")
    polls_mmodel_24.job = 'Knight'
    polls_mmodel_24 = importer.save_or_locate(polls_mmodel_24)

    polls_mmodel_25 = mModel()
    polls_mmodel_25.name = 'Superman'
    polls_mmodel_25.gender = 's'
    polls_mmodel_25.age = 2
    polls_mmodel_25.email = '*****@*****.**'
    polls_mmodel_25.dob = dateutil.parser.parse("2018-06-12")
    polls_mmodel_25.job = 'Knight'
    polls_mmodel_25 = importer.save_or_locate(polls_mmodel_25)

    polls_mmodel_26 = mModel()
    polls_mmodel_26.name = 'Superman'
    polls_mmodel_26.gender = 's'
    polls_mmodel_26.age = 2
    polls_mmodel_26.email = '*****@*****.**'
    polls_mmodel_26.dob = dateutil.parser.parse("2018-06-12")
    polls_mmodel_26.job = 'Knight'
    polls_mmodel_26 = importer.save_or_locate(polls_mmodel_26)

    polls_mmodel_27 = mModel()
    polls_mmodel_27.name = 'Superman'
    polls_mmodel_27.gender = 's'
    polls_mmodel_27.age = 2
    polls_mmodel_27.email = '*****@*****.**'
    polls_mmodel_27.dob = dateutil.parser.parse("2018-06-12")
    polls_mmodel_27.job = 'Knight'
    polls_mmodel_27 = importer.save_or_locate(polls_mmodel_27)

    polls_mmodel_28 = mModel()
    polls_mmodel_28.name = 'Superman'
    polls_mmodel_28.gender = 's'
    polls_mmodel_28.age = 2
    polls_mmodel_28.email = '*****@*****.**'
    polls_mmodel_28.dob = dateutil.parser.parse("2018-06-12")
    polls_mmodel_28.job = 'Knight'
    polls_mmodel_28 = importer.save_or_locate(polls_mmodel_28)

    polls_mmodel_29 = mModel()
    polls_mmodel_29.name = 'Superman'
    polls_mmodel_29.gender = 'g'
    polls_mmodel_29.age = 2
    polls_mmodel_29.email = '*****@*****.**'
    polls_mmodel_29.dob = dateutil.parser.parse("2018-06-12")
    polls_mmodel_29.job = 'Knight'
    polls_mmodel_29 = importer.save_or_locate(polls_mmodel_29)

    polls_mmodel_30 = mModel()
    polls_mmodel_30.name = 'Superman'
    polls_mmodel_30.gender = 'f'
    polls_mmodel_30.age = 2
    polls_mmodel_30.email = '*****@*****.**'
    polls_mmodel_30.dob = dateutil.parser.parse("2018-06-12")
    polls_mmodel_30.job = 'Knight'
    polls_mmodel_30 = importer.save_or_locate(polls_mmodel_30)

    polls_mmodel_31 = mModel()
    polls_mmodel_31.name = 'HOHOH'
    polls_mmodel_31.gender = 'M'
    polls_mmodel_31.age = 2
    polls_mmodel_31.email = '*****@*****.**'
    polls_mmodel_31.dob = dateutil.parser.parse("2018-06-12")
    polls_mmodel_31.job = 'DarkKinght'
    polls_mmodel_31 = importer.save_or_locate(polls_mmodel_31)

    polls_mmodel_32 = mModel()
    polls_mmodel_32.name = 'HOHOH'
    polls_mmodel_32.gender = 'M'
    polls_mmodel_32.age = 2
    polls_mmodel_32.email = '*****@*****.**'
    polls_mmodel_32.dob = dateutil.parser.parse("2018-06-12")
    polls_mmodel_32.job = 'DarkKinght'
    polls_mmodel_32 = importer.save_or_locate(polls_mmodel_32)

    polls_mmodel_33 = mModel()
    polls_mmodel_33.name = 'PowerRangers'
    polls_mmodel_33.gender = 'M'
    polls_mmodel_33.age = 24
    polls_mmodel_33.email = '*****@*****.**'
    polls_mmodel_33.dob = dateutil.parser.parse("2018-05-12")
    polls_mmodel_33.job = 'Super Junior Heroes'
    polls_mmodel_33 = importer.save_or_locate(polls_mmodel_33)

    polls_mmodel_34 = mModel()
    polls_mmodel_34.name = 'Sherlock Holmes'
    polls_mmodel_34.gender = 'M'
    polls_mmodel_34.age = 44
    polls_mmodel_34.email = '*****@*****.**'
    polls_mmodel_34.dob = dateutil.parser.parse("1901-03-14")
    polls_mmodel_34.job = 'Detective'
    polls_mmodel_34 = importer.save_or_locate(polls_mmodel_34)

    polls_mmodel_35 = mModel()
    polls_mmodel_35.name = 'Meng'
    polls_mmodel_35.gender = 'M'
    polls_mmodel_35.age = 31
    polls_mmodel_35.email = '*****@*****.**'
    polls_mmodel_35.dob = dateutil.parser.parse("1987-04-01")
    polls_mmodel_35.job = 'Software Dev'
    polls_mmodel_35 = importer.save_or_locate(polls_mmodel_35)

    polls_mmodel_36 = mModel()
    polls_mmodel_36.name = 'ABCDE'
    polls_mmodel_36.gender = 'M'
    polls_mmodel_36.age = 25
    polls_mmodel_36.email = '*****@*****.**'
    polls_mmodel_36.dob = dateutil.parser.parse("2018-06-20")
    polls_mmodel_36.job = 'CSS Dev'
    polls_mmodel_36 = importer.save_or_locate(polls_mmodel_36)

    polls_mmodel_37 = mModel()
    polls_mmodel_37.name = 'Ali'
    polls_mmodel_37.gender = 'M'
    polls_mmodel_37.age = 24
    polls_mmodel_37.email = '*****@*****.**'
    polls_mmodel_37.dob = dateutil.parser.parse("2018-06-20")
    polls_mmodel_37.job = 'Manager'
    polls_mmodel_37 = importer.save_or_locate(polls_mmodel_37)
Пример #15
0
# -*- coding: utf-8 -*-
from django.contrib.auth.models import User
from polls.models import Question ,Choice
from django.utils import timezone
import random

for q in range(0 ,11):
    question =Question()
    question.question_text ='qst_' +str(q)
    question.pub_date =timezone.now()
    question.save()
    for c in range(0 ,random.randint(1 ,10)):
        choice =Choice()
        choice.choice_text ='chi_' +str(q) +'_' +str(c)
        choice.question =question
        choice.votes =random.randint(3 ,8)
        choice.save()



#>>> from func.filldb import createphoto
#>>> createphoto()
Пример #16
0
q.id
'''
9
'''

# 속성에 접근할 때는 파이썬 문법 그대로 '.'을 사용합니다.
q.question_text
'''
"What's up?"
'''
q.pub_date
'''
datetime.datetime(2020, 5, 15, 12, 38, 16, 448391, tzinfo=<UTC>)
'''

# 기존의 속성값을 변경하고 데이터베이스에 저장합니다.
q.question_text = "What's new ?"
q.save()

# 테이블의 모든 레코드를 조회합니다.
Question.objects.all()
'''
<QuerySet [<Question: What is your hobby?>, <Question: Who do you like best?>, <Question: Where do you live?>, <Question: What's new ?>]>
'''

# 만일 레코드 제목이 [<Question: Question object>]처럼 나온다면,
# models.py 모듈에 __str()__ 메소드를 확인하기 바랍니다.

# 파이썬 쉘을 빠져 나오기 위해서는, exit() 또는 Ctrl+Z(리눅스에서는 Ctrl+D)를 입력합니다.
exit()
Пример #17
0
# (나) https://stackoverflow.com/questions/1937622/convert-date-to-datetime-in-python
# https://docs.python.org/ko/3/library/datetime.html
# https://docs.python.org/ko/3/library/datetime.html#datetime-objects
# https://8percent.github.io/2017-05-31/django-timezone-problem/
# https://brownbears.tistory.com/42
# https://wayhome25.github.io/django/2017/03/20/django-ep5-model/
# https://8percent.github.io/2017-05-31/django-timezone-problem/
# https://docs.djangoproject.com/ko/3.1/topics/i18n/timezones/
# https://spoqa.github.io/2019/02/15/python-timezone.html
# http://abh0518.net/tok/?p=635


# 4.2.3 Update - 데이터 수정 #########################################
# SQL 용어로 UPDATE 절에 해당
## 필드 속성 값 수정 후 save() 메소드
q.question_text = 'What is a new show?'  # 메모리 상에서만 변경됨
q.save()  # 변경사항을 데이터베이스에도 저장
## 여러 객체를 한꺼번에 수정하는 update() 메소드
Question.objects.filter(pub_date__year=2018).update(question_text='Everything is the same')


# 4.2.4 Delete - 데이터 삭제 #########################################
# delete() 메서드는 SQL 용어로 DELETE 절에 해당
Question.objects.filter(pub_date__year=2021).delete()
# 출력 결과 (7, {'polls.Choice': 3, 'polls.Question': 4})  # 없으면 (0, {})
Question.objects.all().delete()  # 모두 지움
# .objects.delete() << 이렇게는 안 됨


# 4.2.5 polls 애플리케이션의 데이터 실습
# 추가로 레코드를 입력하고 확인하는 작업