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

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.

Related

Check which fields are changed in Django

I have a model with many fields. I need to run a code only when two specific fields are changed. I know I can modify my model's save() method but I don't know how to check which fields are changed in my ModelForm.
Sorry because i cant give you any code snippet since i am not very experienced in Django, but i would go for getting a signal when a change is done through a controler class listening on the models and hearing for changes. Hope i might helped a bit.
Good luck
Django-model-utils has something called a FieldTracker that does exactly this. Just instantiate it in your model and tell it to track the specific fields you're looking for. Then you can use the has_changed method to test if a given field has changed since the last save.
This package has some other great utilities as well, I really recommend looking through the docs.
You could try django-dirtyfields.

Are Sortable inlines on Grappelli applicable for M2M fields?

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.

Extending Django's comments framework with Django's users and custom user profile

I'm running Django v1.4.
I looked at the source code for django.contrib.django.comments and noticed that the Comment class has an optional user field that is a foreign key to Django's User. I have also extended User with my own UserProfile which has a user_type field (using the official recommended approach).
I want to place comments for every work order, but only allow certain user_type to be able to post comments. Therefore right now I am thinking of extending Comment to do 2 things:
Only logged in users can post comments. Therefore Comment.user must be mandatory.
Only certain user_type can post comments.
I know I probably have to create my own class and inherit Comment, but I have a few questions in the design:
Should I leave all the optional fields of Comment (user_name, user_email, user_url, etc.) intact? And if so, should I add them with the information from User? I feel if I add them, then it violates data normalization.
How do I restrict comments with only certain UserProfle.user_type? I understand that comments are loaded in the templates, so how should I control if in the template with if-then blocks? That seems to violate the MVC model design (I feel the permission restriction should go in the views.py.
Any tips, suggestions, and references, would be greatly appreciated. Thanks in advance! So far, I have been using the Django documentation about customizing the comments framework as examples.

Custom Field Type or Logic in the View?

Before I start on this project I want make sure what I am proposing is feasible. There is an unusual requirements I have to satisfy.
The model and formModel fields have to support a comment field on every field. I would like to be able to access them by object.field and object.field.comment. At first glance, this looks like I should have a separate type of comment and then somehow link it to correct field but is there a way to create a custom modelField that stores data in more then one database field from one or more form inputs? I would think there would have to be a custom widget to support this too?
If anyone has any ideas or examples of anything like this please respond.
I think this is the answer to my question. I have not tried it yet, but from the description it looks like what I need. https://docs.djangoproject.com/en/dev/ref/forms/widgets/#multiwidget

Pre-Filling a ForeignKey from a link to the Admin?

Some introduction. I have a "planet-like" feed aggregator with an extra layer on top. That extra layer allows for comments and elaboration on the aggregated posts. Here's some code for reference.
class Post(models.Model):
title = models.CharField()
published = models.DateTimeField()
class Story(models.Model):
title = models.CharField()
post = models.ForiegnKey(Post)
Story has a ForeignKey to Post and when I write a story I pick a post from the drop-down. Now, after a few weeks the list could get pretty unruly. I could use raw_id_fields, but that's a bit counter-intuitive since I would have to find the ID of the post I needed.
EDIT: After doing some research, I removed my misleading question. I'm wondering if something like this is possible (given that application is the name of my... application.
Write about this post.
Let me know if THIS needs any more explanation. :)
Looks like the admin recognizes GET values. So,
/admin/application/story/add/?post=[post.id]
would set post to the proper ID. :)
You might want to think about using an autocomplete field instead of raw_id_fields.
Jannis Leidel has a good explanation, with examples, on how to add auto-complete functionality to the djando admin exactly for cases like yours.
You will need to add jquery to the mix, but the process is not that complicated.