Login

Add example content with BaseCommand

Author:
magik_cypress
Posted:
August 14, 2012
Language:
Python
Version:
1.4
Score:
0 (after 0 ratings)

Add a dummy text for your tests

Copy/Paste script into management app content/management/commands/importcontent.py

Usage: python manage.py importcontent 40

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from django.core.management.base import BaseCommand
from content.models import Content
import sys
import random

class Command(BaseCommand):

    def _get_string(self, prompt, reader_func=raw_input, required=True):
        """Helper method to get a non-empty string.
        """
        string = ''
        while not string:
            string = reader_func(prompt + ': ')
            if not required:
                break
        return string

    def handle(self, **kwargs):
        n = self._get_string('Number')
        # random_number = random.randint(0,400)
        # n = random_number
        words = open('/usr/share/dict/words').readlines()

        title= []
        for i in range(2):
            title.append(words[random.randrange(0, len(words))][:-1])

        if title is not None:
            title = ' '.join(title)

        paragraph = []
        for i in range(int(n)):
            paragraph.append(words[random.randrange(0, len(words))][:-1])

        if paragraph is not None:
            paragraph = ' '.join(paragraph)

        content = Content(title=title, paragraph=paragraph)
        content.save()

        print 'Content successfully added'

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.