django-categories icon indicating copy to clipboard operation
django-categories copied to clipboard

Difficulty setting up

Open tereza opened this issue 13 years ago • 12 comments

Django-categories appears to be the perfect solution for my needs. I followed the instructions for installation and setup meticulously.

  1. I installed it, per instructions: pip install django-categories
  2. I added the apps to INSTALLED_APPS.
  3. I registered Many-to-Many Category fields in settings.py using a "lazy connection":
CATEGORIES_SETTINGS = {
'M2M_REGISTRY': {
    'resources.ResourceEntry': (
        {'name': 'audience', 'related_name': 'audiences'},
        {'name': 'diagnosis','related_name': 'diagnoses'},
    ),
    }
}
  1. I ran "python manage.py add_category_fields" both with and without specifying an app, but I always get the error:
ImportError: cannot import name MODEL_REGISTRY
in File "/home/tereza/t42/lib/python2.5/site-packages/categories/management/commands/add_category_fields.py", line 23 

which can be avoided if I add ".settings" to line 23, like so:

        from categories.settings import MODEL_REGISTRY 
  1. but then I get a new error:
TypeError: migrate_app() takes at least 4 arguments (1 given) 

I'm pretty much at a loss here. Why is the add_category_fields command incorrect? Am I missing something that would be obvious to a real Pythonista or Djangonaut?

tereza avatar Sep 27 '12 17:09 tereza

Forgot to mention: Using Django 1.4.1, South 0.7.6, Django-categories 1.1.4

tereza avatar Sep 27 '12 17:09 tereza

Can you try just running ./manage.py syncdb --migrate.

jsoa avatar Sep 27 '12 18:09 jsoa

It created a M2M table for "categories", but don’t tables have to be created for my M2M "Audience" and "Diagnosis" models? Isn’t that what "add_category_fields" is for?

Or have I misunderstood its purpose?

t

On Sep 27, 2012, at 1:04 PM, Jose Soares wrote:

Can you try just running ./manage.py syncdb --migrate.

— Reply to this email directly or view it on GitHub.

tereza avatar Sep 27 '12 18:09 tereza

Actually, Many-To-Many relations are not connected by a foreign key. A intermediate table is created that has a foreign key to categories and to the other model.

You should have an intermediary table between Category and ResourceEntry.

You have, it seems, discovered a bug in the add_category_fields command, but it shouldn't be relevant for your setup.

If you run your application and go to the ResourceEntry model, do you get an error, or do you see your category fields?

Corey

On Sep 27, 2012, at 1:55 PM, Tereza Snyder [email protected] wrote:

It created a M2M table for "categories", but don’t tables have to be created for my M2M "Audience" and "Diagnosis" models? Isn’t that what "add_category_fields" is for?

Or have I misunderstood its purpose?

t

On Sep 27, 2012, at 1:04 PM, Jose Soares wrote:

Can you try just running ./manage.py syncdb --migrate.

— Reply to this email directly or view it on GitHub.

— Reply to this email directly or view it on GitHub.

coordt avatar Sep 27 '12 19:09 coordt

I do see the category model, but I'm not actually using the category app, Instead I intend to use my Audience and Diagnosis classes based on CategoryBase. In an early attempt to get past this error, I added categories to INSTALLED_APPS in addition to categories.editor. At least I could test whether it would create an intermediate table. It did. So I expected add_category_fields to do the same for my CategoryBase based models as well. But...

tereza avatar Sep 27 '12 19:09 tereza

Ok, I think I understand now.

The CATEGORIES_SETTINGS only impacts the categories app. It does nothing for subclasses of CategoryBase.

Assuming that the ResourceEntry is one of your models, you can add the custom admin classes for audience and diagnosis to the ResourceEntry admin inlines.

If you can't modify ResourceEntry's admin class, I can tell you how to do it dynamically, like categories does, but it is more complex.

Corey

On Sep 27, 2012, at 2:25 PM, Tereza Snyder [email protected] wrote:

I do see the category model, but I'm not actually using the category app, Instead I intend to use my Audience and Diagnosis classes based on CategoryBase. In an early attempt to get past this error, I added categories to INSTALLED_APPS in addition to categories.editor. At least I could test whether it would create an intermediate table. It did. So I expected add_category_fields to do the same for my CategoryBase based models as well. But...

— Reply to this email directly or view it on GitHub.

coordt avatar Sep 27 '12 19:09 coordt

Aha! That explains it. I was so looking for a way to attach different sets of categories to the SAME model that I willfully misread the docs.

I am trying mightily to keep my ResourceEntry model self-contained. I would really appreciate directions for how to keep it that way.

t

tereza avatar Sep 27 '12 19:09 tereza

Ok, You'll have to do some copying and pasting from the categories app.

In categories.registration has the basic code for modifying other apps, if you want to browse it.

  1. Basically, you need to create an instance of a ManyToManyField, as if it were part of a model.

field = models.ManyToManyField(Audience)

  1. Then call the field's contribute_to_class to add it to the ResourceEntry model

field.contribute_to_class(ResourceEntry, 'audience')

  1. Then you'll need to modify the ResourceEntry's admin class. For that, it will be easier to create a new one and swap it out.

class NewResourceAdmin(admin.ModelAdmin): inlines = [AudienceInline,]

admin.sites.unregister(ResourceEntry) admin.sites.register(ResourceEntry, NewResourceAdmin)

I hope this is enough for you to go on. Let me know if you need more help.

Corey

On Sep 27, 2012, at 2:44 PM, Tereza Snyder [email protected] wrote:

Aha! That explains it. I was so looking for a way to attach different sets of categories to the SAME model that I willfully misread the docs.

I am trying mightily to keep my ResourceEntry model self-contained. I would really appreciate directions for how to keep it that way.

t

— Reply to this email directly or view it on GitHub.

coordt avatar Sep 27 '12 20:09 coordt

Thanks. I look forward to conquering this. And if there's really is a bug in add_category_fields, at least I unearthed it for you ;-).

If I succeed, I'll write it up for the docs.

tereza avatar Sep 27 '12 20:09 tereza

That would be wonderful. Let me know if you need anything

Sent from my iPhone

On Sep 27, 2012, at 3:25 PM, Tereza Snyder [email protected] wrote:

Thanks. I look forward to conquering this. And if there's really is a bug in add_category_fields, at least I unearthed it for you ;-).

If I succeed, I'll write it up for the docs.

— Reply to this email directly or view it on GitHubhttps://github.com/callowayproject/django-categories/issues/57#issuecomment-8952743.

coordt avatar Sep 27 '12 21:09 coordt

something new on this issue?

rmaceissoft avatar Feb 28 '13 01:02 rmaceissoft

i have fallen into the same problem as the original poster, I don't want to modify the 3rd party app but would like to add categories relationship to it.

variable avatar May 20 '13 03:05 variable