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

'CloneMixin' has no attribute '_meta' #58

Closed
rafrasenberg opened this issue Feb 15, 2020 · 13 comments
Closed

'CloneMixin' has no attribute '_meta' #58

rafrasenberg opened this issue Feb 15, 2020 · 13 comments

Comments

@rafrasenberg
Copy link

Hi,

I am using your package, very usefull! However I get this error after resetting my database and trying to make migrations. Any ideas what is causing this error?

When I already had the model in my database and then added CloneMixin to my model it was working. But with an empty database the make migrations is not working.

@jackton1
Copy link
Member

Thanks

Could you include the traceback of the error you saw ??

Would be really helpful.

@rafrasenberg
Copy link
Author

@jackton1 thankyou for your quick response.

See the test:

(frider) raf@webconexus:~/Development/frider/src/frider$ django m
Operations to perform:
  Apply all migrations: account, address, admin, auth, company, contenttypes, sessions, sites, socialaccount, users
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0001_initial... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying users.0001_initial... OK
  Applying account.0001_initial... OK
  Applying account.0002_email_max_length... OK
  Applying address.0001_initial... OK
  Applying address.0002_auto_20160213_1726... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying company.0001_initial... OK
  Applying sessions.0001_initial... OK
  Applying sites.0001_initial... OK
  Applying sites.0002_alter_domain_unique... OK
  Applying sites.0003_set_site_domain_and_name... OK
  Applying socialaccount.0001_initial... OK
  Applying socialaccount.0002_token_max_lengths... OK
  Applying socialaccount.0003_extra_data_default_dict... OK
(frider) raf@webconexus:~/Development/frider/src/frider$ django mm
Traceback (most recent call last):
  File "/home/raf/Development/frider/src/frider/manage.py", line 30, in <module>
    execute_from_command_line(sys.argv)
  File "/home/raf/Development/frider/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/raf/Development/frider/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/raf/Development/frider/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/raf/Development/frider/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/home/raf/Development/frider/lib/python3.7/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/raf/Development/frider/lib/python3.7/site-packages/django/core/management/commands/makemigrations.py", line 168, in handle
    migration_name=self.migration_name,
  File "/home/raf/Development/frider/lib/python3.7/site-packages/django/db/migrations/autodetector.py", line 43, in changes
    changes = self._detect_changes(convert_apps, graph)
  File "/home/raf/Development/frider/lib/python3.7/site-packages/django/db/migrations/autodetector.py", line 196, in _detect_changes
    self._optimize_migrations()
  File "/home/raf/Development/frider/lib/python3.7/site-packages/django/db/migrations/autodetector.py", line 372, in _optimize_migrations
    migration.operations = MigrationOptimizer().optimize(migration.operations, app_label=app_label)
  File "/home/raf/Development/frider/lib/python3.7/site-packages/django/db/migrations/optimizer.py", line 35, in optimize
    result = self.optimize_inner(operations, app_label)
  File "/home/raf/Development/frider/lib/python3.7/site-packages/django/db/migrations/optimizer.py", line 49, in optimize_inner
    result = operation.reduce(other, app_label)
  File "/home/raf/Development/frider/lib/python3.7/site-packages/django/db/migrations/operations/models.py", line 239, in reduce
    return super().reduce(operation, app_label=app_label)
  File "/home/raf/Development/frider/lib/python3.7/site-packages/django/db/migrations/operations/models.py", line 37, in reduce
    not operation.references_model(self.name, app_label)
  File "/home/raf/Development/frider/lib/python3.7/site-packages/django/db/migrations/operations/models.py", line 111, in references_model
    ModelTuple.from_model(base) == model_tuple):
  File "/home/raf/Development/frider/lib/python3.7/site-packages/django/db/migrations/operations/utils.py", line 32, in from_model
    return cls(model._meta.app_label, model._meta.model_name)
AttributeError: type object 'CloneMixin' has no attribute '_meta'

As you can see i used a complete clean database with no migrations.

@rafrasenberg
Copy link
Author

When I remove CloneMixin from my model and then make my migrations, it works. After my new migrations I put CloneMixin back and all is good. So what can be the issue here?

@jackton1
Copy link
Member

jackton1 commented Feb 15, 2020

Sure, your welcome

Can you test using
https://github.com/jackton1/django-clone/blob/8906343c19048e9592b91a05fc84889f2591ecc9/model_clone/models.py#L6

class MyModel(CloneModel):  # Won't need to add models.Model
    ...

Also a copy of how the model is setup would be helpful.

@rafrasenberg
Copy link
Author

Still the same traceback. My model:

class Assignment(models.Model, CloneMixin):
    created_at = models.DateTimeField(auto_now_add=True, null=True)
    updated_at = models.DateTimeField(auto_now=True, null=True)
    title = models.CharField(max_length=100, null=True, blank=True, verbose_name=_("Job title"))
    assignment_date = models.DateField(blank=True, null=True)
    company = models.ForeignKey('company.CompanyDepot', null=True, blank=True,
                                on_delete=models.CASCADE, verbose_name=_("Company"))
    has_applied = models.ManyToManyField('driver.Driver', blank=True, verbose_name=_(
        "Driver applications"), related_name="driver_applications")
    chosen_driver = models.ManyToManyField('driver.Driver', blank=True, verbose_name=_(
        "Chosen driver"), related_name="driver_chosen")
    assignment_status = models.CharField(max_length=2, choices=ASSIGNMENT_STATUS,
                                         default='O', verbose_name=_("Assignment status"), blank=True)
    location = models.CharField(max_length=25, null=True, verbose_name=_("Location"))
    driver_type = models.CharField(max_length=2, choices=DRIVER_TYPES, null=True, verbose_name=_("Driver type"))
    car_type = models.CharField(max_length=2, choices=CAR_TYPES, null=True, verbose_name=_("Car type"))
    compensation = models.DecimalField(null=True, max_digits=5, decimal_places=2, verbose_name=_("Compensation"))
    hours = models.IntegerField(null=True, verbose_name=_("Amount of hours"))
    spots_available = models.IntegerField(null=True, verbose_name=_("Spots available"))
    description = models.TextField(null=True, blank=True, verbose_name=_("Assignment description"))
    contract = models.ForeignKey('assignment.Contract', null=True, blank=True,
                                 on_delete=models.SET_NULL, verbose_name=_("Choose contract"), related_name="assignment_contracts")

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('assignment-detail', kwargs={'pk': self.pk})

    def get_update_url(self):
        return reverse('assignment-update', kwargs={'pk': self.pk})

    def get_delete_url(self):
        return reverse('assignment-delete', kwargs={'pk': self.pk})

    def get_candidate_url(self):
        return reverse('assignment-candidate-detail', kwargs={'pk': self.pk})

    def get_candidate_delete_url(self):
        return reverse('assignment-candidate-delete', kwargs={'pk': self.pk})

    def get_candidate_update_url(self):
        return reverse('assignment-candidate-update', kwargs={'pk': self.pk})

    
    ## Model clone settings
    _clone_excluded_many_to_many_fields = ['has_applied', 'chosen_driver',]



    class Meta:
        verbose_name = _('Assigment')
        verbose_name_plural = _('Assignments')

This set-up is not working currently. Only when I remove CloneMixin.

@jackton1
Copy link
Member

Oh yeah it's the way you applied the mixin for python they are left -> right

Changing to

class Assignment(CloneMixin, models.Model):
   ...

Should solve the error.

@jackton1
Copy link
Member

@webconexus Please let me know if this solution above solves your issue.

@rafrasenberg
Copy link
Author

@jackton1 I tried that already, but I still got the same error.

@jackton1
Copy link
Member

@webconexus Sorry I haven't been able to get to this can you confirm the current version you have setup.

Django version: ..
Python version: ..
Django clone version: ..

@lahuindai
Copy link

lahuindai commented May 5, 2020

Any update on this. Getting the same
Django==3.0.6
django-clone==0.1.3
Python==3.8.2

Tried both rearranging Mixin and changing to CloneModel neither worked.

@jackton1
Copy link
Member

jackton1 commented May 13, 2020

Ran this here #69 with

Django==3.0.6
django-clone==0.1.3
Python==3.8.2

Can you verify that the python version used in the virtualenv is correct seems it uses Python3.7 instead of Python3.8 as expected.

File "/home/raf/Development/frider/lib/python3.7/site-packages/django/db/migrations/operations/models.py", line 111, in references_model
    ModelTuple.from_model(base) == model_tuple):

@jackton1
Copy link
Member

jackton1 commented May 16, 2020

After trying to replicate this the final conclusion seems to be how your migrations are generated to solve this error try regenerating the migrations for the Assignment model.

Make sure to delete any of the dependencies specified here

    dependencies = [
        ('sample_company', '0001_initial'),
        ('sample_assignment', '0001_initial'),
        ('sample_driver', '0001_initial'),
        ('sample', '0006_assignment'),
    ]

    operations = [
        migrations.AddField(
            model_name='assignment',
            name='applied_drivers',
            field=models.ManyToManyField(blank=True, related_name='driver_applications', to='sample_driver.Driver', verbose_name='Driver applications'),
        ),

before running the migration.

@jackton1
Copy link
Member

jackton1 commented May 21, 2020

#71 Shows an example setup to replicate the issue creating a new release.

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

3 participants