Skip to content

Prevent duplicate items #569

Answered by guacs
Chris-May-WS asked this question in Q&A
Jul 24, 2024 · 1 comments · 4 replies
Discussion options

You must be logged in to vote

Looking at your validator, it seems you need to ensure that the type is unique for each Phone instance right? If so, then I would consider doing something like the following:

class PhoneFactory(ModelFactory[Phone]):
    __set_as_default_factory_for_type__ = True
    
    @classmethod
    def type(cls) -> str:
        try:
            return cls.__faker__.unique.word()
        except UniquenessException:
            cls.__faker__.unique.clear()
            return cls.__faker__.unique.word()

Now unless you're creating a really large list of phones for a single contact, then this should ensure that you get a list of unique Phone instances for a single Contact.

Also, if you use the Faker inst…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@Chris-May-WS
Comment options

@Chris-May-WS
Comment options

@guacs
Comment options

Answer selected by Chris-May-WS
@Chris-May-WS
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants