Are Sortable inlines on Grappelli applicable for M2M fields? - django

I can't seem to get the sortable inlines feature (in grappelli) work for me.
I'm starting to wonder if the feature is not supported for ManyToManyFields or if I am not getting it right.
Also, is there a sample code out there so I can see and learn? I already followed the method described here.

M2M Fields didn't work for me. But, turns out there's a work-around for this.
I created a through model for a ManyToManyField and added order in that through model. Remember, the through model uses ForeignKey fields. So, the case essentially becomes that of managing a couple of one-to-many relationships.
Implementing sortable inlines is now as easy as following Grappelli's documentation.

Related

Creating Model Relationships Via Forms

What is the defacto way of creating model relationships in Django via frontend forms.
For example a user signs up for service using a form, they start a quote.
In getting a quote they can select and add products to their quote specifying variable such as sizes in this process.
This is modelled with relevant User, Quote, Product models and relevant relationships.
I am trying to work out the best way that these are linked together by frontend forms and views.
Would I load into the quote form a hidden field for the related user_id for example, which I can then process manually to form the one-to-many relationship.
I am just wondering if this is something accounted for within forms or if I have to manually create the forms to achieve my goal.
This is one of the more complicated things to try and achieve but there are several things in Django which will help you.
You're going to need a ManyToMany field on the Quote model to link the Products to it.
This can be displayed in forms simply via a ModelMultipleChoiceField:
https://docs.djangoproject.com/en/dev/ref/forms/fields/#modelmultiplechoicefield
... which is just renders a basic multiple select list of existing products.
The interface you want probably looks more like an inline formset however. The complication here is that they are designed for ForeignKey relations rather than ManyToMany.
Under the covers, a ManyToMany relation is actually just two ForeignKey relations, via an intermediate 'through' model. We can exploit this to build an inline formset on the through model, see this answer:
https://stackoverflow.com/a/10999074/202168
You'll note the caveat in that answer, the inline rows won't know which Quote they belong to unless you override some code.
You may like to look at some helper apps which provide custom widgets for ManyToMany fields:
https://code.google.com/p/django-ajax-filtered-fields/
http://django-autocomplete-light.readthedocs.org/en/latest/

Which model should be higher?

Which model should be higher? I still have a error.
class Post(models.Model):
blog = models.ForeignKey(Blog)
class Blog(models.Model):
post = models.ManyToManyField(Post,blank=True,null=True)
Essentially, you don't need the foreign key from Post -> Blog. See the docs. ManyToMany give you the reverse direction as part of their default behaviour.
Edit (as per #Tony Blundell), you can specify models via a string for foreign keys in cases of order-of-definition issues. However, in this case this (I assume) this isn't what you are trying to do. In your case, scrap the ForeignKey under Post, and then you can make use of the reverse behaviour of M2M to represent that relationship.
Also I'd check out the docs, as they are really good for the Django project and cover all of the basics. The Tutorial, if you haven't done it yet, is very useful.

Django Haystack RealTimeSearchIndex on ManyToMany Relationships strange behaviour

Following a related (as yet unanswered) question, I did some investigation and found that the current implementation of Django Haystack's RealTimeSearchIndex makes no attempt to also update on related field (Many to Many) changes. I thought this would be an easy fix - after all, I could just extend RealTimeSearchIndex like this:
class RealTimeM2MSearchIndex(RealTimeSearchIndex):
def _setup_save(self, model):
signals.m2m_changed.connect(self.update_object, sender=model)
signals.post_save.connect(self.update_object, sender=model)
But then I realized (or at least assumed, since it's not working) that this only works if the M2M field is defined on the model itself, and not if it's the "reverse" side of the M2M relationship. Trying to fix that, I then did something like the following:
signals.m2m_changed.connect(self.update_object, sender=model.related_field.through)
Where related_field is the name of the specific Model on other side of the ManyToMany definition. Strangely enough, upon running, Django then complains that the Model has no such field, related_field.
And sure enough, if I inspect the object, Django has not yet extended the model to have the related_field field. If I inspect the same object when displaying a view, however, it does have that related_field.
Summary
So the problem seems to be that Django's automatic behavior to add an attribute to the reverse side of an M2M relationship has yet to happen when Haystack runs its code. How can I overcome this obstacle, and allow Haystack's RealTimeSearchIndex to also update on related field changes?
I think the simplest solution is to just use the built in RealTimeSearchIndex, and add a signal listener in your models.py to reindex the model on m2m_changed, or whenever. See my answer to the other question - you could easily modify it to index on m2m_changed instead of post_save.
Just tried implementing this myself, and your problem is the value of the sender argument in this line:
signals.m2m_changed.connect(self.update_object, sender=model)
I read the documentation for the m2m_changed signal and the sender will be something like MyModel.my_field.through so you need to use that. This means you can't have a generic class as you are trying to do, but will need to define the _setup_save method in each case, with the m2m_changed signal connected for each ManyToMany field that model has.
For example, if your model had two ManyToManyFields called region and sector, you could do:
# we implement these to force the update when the ManyToMany fields change
def _setup_save(self, model):
signals.m2m_changed.connect(self.update_object,
sender=MyModel.sector.through)
signals.m2m_changed.connect(self.update_object,
sender=MyModel.region.through)
signals.post_save.connect(self.update_object, sender=model)
You should also really define the _teardown_save() method:
def _teardown_save(self, model):
signals.m2m_changed.disconnect(self.update_object,
sender=MyModel.sector.through)
signals.m2m_changed.disconnect(self.update_object,
sender=MyModel.region.through)
signals.post_save.disconnect(self.update_object, sender=model)
(This is based on code I have tested, it appears to work - certainly no errors about fields not existing).
Update: Just read your question more closely. Is it possible that your model has the ManyToManyField added dynamically? Is the call to register() after you have defined all your classes?

Filter through a related model django

How can I generate a query_set through a related model?
For example, how can I do this:
UserProfile.objects.filter(user.is_active=True) # Can't use user.is_active to filter
Trivial question, trivial answer. But I'll keep it here for posterity's sake.
UserProfile.objects.filter(user__is_active=True)
This is well documented in the Django docs.
From the Django documention
Django offers a powerful and intuitive way to "follow" relationships in lookups, taking care of the SQL JOINs for you automatically, behind the scenes. To span a relationship, just use the field name of related fields across models, separated by double underscores, until you get to the field you want.
In your example this would be:
UserProfile.objects.filter(user__is_active=True)
The easiest way to follow a relationship is to use a simple "__".
UserProfile.objects.filter(user__is_active=True)
These can be changed together as well (ie user_parent_email='abc#def.com')

Customize Django comments framework so that the comment does not have to be unique

I'm customizing the comments model per the Django documentation.
In my specific use case, however, comments are allowed to be blank. The trouble I get into then is that the Comment model is setup with an unique_together:
unique_together = [('user', 'comment', 'flag')]
Any ideas on how I could go about overriding this?
(...or did I start off on the wrong track with using the Comments framework altogether? :)
Doesn't look like the Comment model has a unique constraint.
Code for models.py for contrib.comments.
It looks like the CommentFlag model has the uniqueness constraint which shouldn't effect you having blank comments.
Your problem must lie elsewhere.
I'm not very familiar with the comments app but here are some ideas you can look at to get around your problem.
Warning I haven't used either of these methods on the comments app so I'm not sure if using these will break any downstream functions of the comments framework. Be sure to look into/test if you decide to use either of these.
That being said, I can think of 2 ways you can approach this.
Override the unique together:
class NonUniqueComment(Comment):
class Meta(Comment.Meta):
unique_together = []
Make the comments field store Null instead of empty string in the db.