Login

Add example content with BaseCommand

Author:
magik_cypress
Posted:
August 14, 2012
Language:
Python
Version:
1.4
Tags:
Management Command BaseCommand
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. Add example contact with BaseCommand by magik_cypress 2 years, 10 months ago
  2. Custom Command for Rebuilding Permissions and ContentTypes by cronosa 4 years, 3 months ago
  3. extras.py for management commands by dnordberg 7 years, 10 months ago
  4. Testing for failure in management commands by David 4 years, 10 months ago
  5. Simple snippet to generate whole project models uml diagram in YUML.me format by nikolajusk 4 years, 9 months ago

Comments

Please login first before commenting.