gregmuellegger/django-autofixture

Better support for choices, null=True and blank=True

Opened this issue · 0 comments

I have the following model at my app

BLOG_POST_STATUS = (
    ('U', 'Waiting for triage'),  # This is the status after we receive the blog post draft.
    ('R', 'Waiting to be reviewed'),  # Blog post is assigned to one staff to be reviewed.
    ('G', 'Waiting to be proofread'),  # Blog post is assigned to be proofread by the community officer.
    ('L', 'Waiting to be published'),  # Blog post will be publish by the community officer.
    ('P', 'Published'),  # Blog post is published and have a URL at the website.
    ('D', 'Declined'),  # Blog post submitted by mistake.
    ('O', 'Out of date'),  # Blog post that wait too long to be publish for any reason.
    ('X', 'Remove'),  # When the fellow decided to remove their request.
)

class Blog(models.Model):
    status = models.CharField(
        choices=BLOG_POST_STATUS,
        max_length=1,
        default="U"
    )
    title = models.CharField(
        max_length=MAX_CHAR_LENGTH,
        null=True,
        blank=True
    )
    published_url = models.CharField(
        max_length=MAX_CHAR_LENGTH,
        null=True,
        blank=True
    )
    tweet_url = models.CharField(
        max_length=MAX_CHAR_LENGTH,
        null=True,
        blank=True
    )

    added = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    def save(self, *args, **kwargs):  # pylint: disable=arguments-differ
        if self.published_url:
            self.status = 'P'
        super(Blog, self).save(*args, **kwargs)

After I run

$ python --version
Python 3.6.0 :: Continuum Analytics, Inc.
$ python manage.py shell
Python 3.6.0 |Continuum Analytics, Inc.| (default, Dec 23 2016, 12:22:00) 
Type 'copyright', 'credits' or 'license' for more information
>>> import django
>>> django.__version__
'1.11.3'
>>> import autofixture
>>> autofixture.__version__
'0.12.1'
>>> exit()
$ python manage.py loadtestdata myapp.Blog:120

I ended up with 120 Blog entries but all has a not null published_url and status equals to P. Would be great to have a more random generator.