Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rgaiacs opened this issue Jul 27, 2017 · 0 comments
Open

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

rgaiacs opened this issue Jul 27, 2017 · 0 comments

Comments

@rgaiacs
Copy link

rgaiacs commented Jul 27, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant