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

Master model duplicated - Rollback if translation cannot be saved ? #357

Open
remychvn opened this issue Mar 18, 2019 · 1 comment
Open

Comments

@remychvn
Copy link

Hi,

I've got a problem. I have this model:

class DestinationManager(TranslationManager, TreeManager):
    use_for_related_fields = True


class Destination(MPTTModel, TranslatableModel):
    CONTINENT = 'CONTINENT'
    CONTINENT_SECTION = 'CONTINENT_SECTION'
    COUNTRY = 'COUNTRY'
    REGION = 'REGION'
    CITY = 'CITY'
    AREA = 'AREA'

    DESTINATION_TYPE = (
        (CONTINENT, CONTINENT),
        (CONTINENT_SECTION, CONTINENT_SECTION),
        (COUNTRY, COUNTRY),
        (AREA, AREA),
        (REGION, REGION),
        (CITY, CITY)
    )

    parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')
    area_type = models.CharField(
        max_length=25,
        choices=DESTINATION_TYPE,
        default=COUNTRY,
        db_index=True
    )

    translations = TranslatedFields(
        name=models.CharField(max_length=50, unique=True, db_index=True)
    )

    objects = DestinationManager()

I would like the master model not to be added if the translation cannot be added. I thought that was the default behavior, but it doesn't seem to be the case.
Is there an easy way to do that ?

Thank you for this great library by the way.

@remychvn
Copy link
Author

remychvn commented Mar 31, 2019

So I encapsulated the creation of objects and their translation in a transaction.
Is that the recommended way to do it? 🤔

Is there a particular reason why this behavior is not the default?

           with transaction.atomic():
                try:
                    destination = Destination.objects.language(language_code).create(
                        name=country_name, area_type=Destination.COUNTRY
                    )
                except IntegrityError:
                    continue

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