question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to create Profile.username with faker?

See original GitHub issue

I wanna create random value with faker wrapper, however, I can’t get extract any value from it.

# not working
username = factory.Faker('profile')['username']

Has anyone done this before?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

40reactions
rbarroiscommented, Dec 2, 2017

It’s a bit hidden, but Faker has a user_name provider: http://faker.readthedocs.io/en/latest/providers/faker.providers.internet.html?highlight=user_name

=> You can use factory.Faker('user_name') 😉

5reactions
etiologycommented, Oct 18, 2017

Well I have a not super great solution. It adds an extra call so maybe someone can clean it up:

import factory
from faker import Faker

fake = Faker()

def generate_username(*args):
    """ returns a random username """
    return fake.profile(fields=['username'])['username']

class Profile(object):
    """ Simple Profile model """
    def __init__(self, **kwargs):
        self.username = kwargs.get('username')

class ProfileFactory(factory.Factory):
    """ Generate random profile instances """
    class Meta:
        model = Profile
    username = factory.LazyAttribute(generate_username)

Test

for x in range(10):
    a = ProfileFactory()
    print(a.username)

Output:

reeselinda
arroyozachary
gonzalescatherine
xcarpenter
jcohen
zsimmons
stokestyler
ofletcher
monicaphelps
johnharris

So the reason I went with a LazyAttribute is that if you don’t delay the call of a function in the factory then the items generated won’t be random. In my experience the first ‘username’ will be random but the rest will just repeat it. Given this behavior I decided to delay the function call until instantiation so that each instance gets a random name

Read more comments on GitHub >

github_iconTop Results From Across the Web

faker.providers.profile — Faker 15.3.4 documentation
This provider is a collection of functions to generate personal profiles and identities. ... Built with Sphinx using a theme provided by Read...
Read more >
How to create fake user profiles using faker in Python
The attributes of the profile generated are as follows: username; name; sex; address; mail; birthdate. Method signature. simple_profile(sex ...
Read more >
How to use userName function in faker - Javascript - Tabnine
const generateUser = () => { const createdAt = faker.date.past(2); const name = faker.name.findName(); return { id: uuid(), name, ...
Read more >
Python Faker.user_name Examples
Python Faker.user_name - 30 examples found. These are the top rated real world Python examples of faker.Faker.user_name extracted from open source projects.
Read more >
How to Create Fake Data with Faker | by Khuyen Tran
fake = Faker()fake.profile(). As we can see, most relevant information about a person is created with ease, even with mail, ssn, username, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found