I cannot find a replacement for the LinkColumn in the new versions of django-tables2. Author states that LinkColumn is deprecated and shouldn't be used. But the new linkify solution is poorly documented and doesn't have all the features of the old version. For example, I have this column:
edit = tables.LinkColumn(
'wagtailadmin_pages:edit', args=[A('page.pk')],
text='Edit'
)
It displays a link to the wagtail admin edit page called Edit. There's simply no way to achieve the same using linkify because linkify only works if you have valid accessor on the column. But accessor cannot return same static text for all rows (unless I modify the model to add a dummy property - but this particular model is in the 3rd party package and it would feel like a duct tape solution anyway).
In all other cases, column will not display a link. I've studied the source code and it seems that such case is simply not supported by the django-tables2 > 2.0.0.
Is there any clean and understandable way to construct a link column with a static link text using linkify?
Answering my own question. It seems that it is impossible to fully replace LinkColumn with the linkify feature. The following code solves my problem:
from django.urls import reverse
from django.utils.text import mark_safe
import django_tables2 as tables
from wagtail.core.models import PageRevision
class WagtailRevisionsTable(tables.Table):
title = tables.Column(
accessor='page.title',
linkify=lambda record: record.page.url,
verbose_name='Title'
)
edit = tables.Column(
accessor='page.pk'
)
class Meta:
model = PageRevision
fields = ('title', 'created_at', 'user', 'edit')
template_name = 'django_tables2/bootstrap-responsive.html'
def render_edit(self, value):
url = reverse('wagtailadmin_pages:edit', args=[value])
return mark_safe(f'Edit')
The code with the old LinkColumn was much more concise, I don't understand the reason for change and documentations really doesn't help. There's simply not enough information on linkify or render_col methods.
So I hope this answer will help some poor soul trying to port old code to the django-tables2 >= 2.0.
Related
I recently added a new model to my site, and I'm using an admin.py file to specify exactly how I want it to appear in the admin site. It works great, but I can't figure out how to get one of my date fields to include seconds in it's display format. I'm only seeing values like "Aug. 27, 2011, 12:12 p.m." when what I want to be seeing is "Aug. 27, 2011, 12:12*:37* p.m."
Try this in the ModelAdmin:
def time_seconds(self, obj):
return obj.timefield.strftime("%d %b %Y %H:%M:%S")
time_seconds.admin_order_field = 'timefield'
time_seconds.short_description = 'Precise Time'
list_display = ('id', 'time_seconds', )
Replacing "timefield" with the appropriate field in your model, of course, and adding any other needed fields in "list_display".
digging around I ended here but applied a different approach to my app.
Changing django admin default formats could be done changing the django locale formats for every type you want.
Put the following on your admin.py file (or settings.py) to change datetime default format at your django admin.
from django.conf.locale.es import formats as es_formats
es_formats.DATETIME_FORMAT = "d M Y H:i:s"
It will change the ModelAdmin's datetime formats on that file (or whole site if in settings).
It does not breaks admin datetime filters and order features as #Alan Illing has point out in comments .
hope this help in future
Extra info:
You can change it for every available locale in django, which are a lot.
You can change the following formats using this approach
from django.conf.locale.es import formats as es_formats
es_formats.DATETIME_FORMAT
es_formats.NUMBER_GROUPING
es_formats.DATETIME_INPUT_FORMATS
es_formats.SHORT_DATETIME_FORMAT
es_formats.DATE_FORMAT
es_formats.SHORT_DATE_FORMAT
es_formats.DATE_INPUT_FORMATS
es_formats.THOUSAND_SEPARATOR
es_formats.DECIMAL_SEPARATOR
es_formats.TIME_FORMAT
es_formats.FIRST_DAY_OF_WEEK
es_formats.YEAR_MONTH_FORMAT
es_formats.MONTH_DAY_FORMAT
If you've tried gabriel's answer but it did not work, try to set USE_L10N = False in settings.py, it works for me.
Note that if USE_L10N is set to True, then the locale-dictated format has higher precedence and will be applied instead
See: https://docs.djangoproject.com/en/2.0/ref/settings/#std:setting-DATETIME_FORMAT
The accepted answer is correct, however I found it a bit confusing to understand how/why it works. Below is a small example that I hope illustrates how to do this more clearly.
Django provides a few ways to display "custom" fields in your admin view. The way I prefer to achieve this behavior is to define a custom field in the ModelAdmin class and display that instead of your intended field:
from django.contrib import admin
from django.db import models
class Person(models.Model):
name = models.CharField(max_length=50)
birthday = models.DateField()
class PersonAdmin(admin.ModelAdmin):
#admin.display(description='Birthday')
def admin_birthday(self, obj):
return obj.birthday.strftime('%Y-%m-%d')
list_display = ('name', 'admin_birthday')
Notice that instead of displaying the actual birthday field from the Person model, we define a custom field (admin_birthday) as a method in the PersonAdmin and display that instead by adding it to the list_display attribute. Furthermore, the admin.display() decorator modifies how Django will display this custom field in the admin view. Using this approach, the admin panel will show the NAME and BIRTHDAY fields but using your preferred date formatting for the date.
The reason I prefer this approach is you keep the Model field definitions separate from how you display them in the admin panel. You can read more about alternative approaches in the Django admin documentation.
I'm having some issues using the Algolia Django integration with one of my models which contains a TaggitManager() field. I'm currently being thrown back the following error when running this command:
$ python manage.py algolia_reindex
AttributeError: '_TaggableManager' object has no attribute 'name'
I've had a look at the Taggit documentation, but I'm just not sure exactly how I would marry the method outlined with the Algolia search index method.
index.py:
import django
django.setup()
from algoliasearch_django import AlgoliaIndex
class BlogPostIndex(AlgoliaIndex):
fields = ('title')
settings = {'searchableAttributes': ['title']}
index_name = 'blog_post_index'
models.py:
from taggit.managers import TaggableManager
class Post(models.Model):
...some model fields...
tags = TaggableManager()
To index the taggit tags with your Post fields, you will need to expose a callable that returns a Blog Post's tags as a list of strings.
The best option is to store them as _tags, which will let you filter on tags at query time.
Your PostIndex would look like this:
class PostIndex(AlgoliaIndex):
fields = ('title', '_tags')
settings = {'searchableAttributes': ['title']}
index_name = 'Blog Posts Index'
should_index = 'is_published'
As for Post:
class Post(models.Model):
# ...some model fields...
tags = TaggableManager()
def _tags(self):
return [t.name for t in self.tags.all()]
Following these instructions, your records will be indexed with their respective tags:
You can check the taggit branch of our Django demo, which demonstrates these steps.
To answer my own question. I have now passed in both the model and the model index so Algolia now knows what to index and what not to index. Although I would like a method to allow Algolia to index taggit tags, alas, it is probably not possible.
My apps.py file:
import algoliasearch_django as algoliasearch
from django.apps import AppConfig
from .index import PostIndex
class BlogConfig(AppConfig):
name = 'blog'
def ready(self):
Post = self.get_model('Post')
algoliasearch.register(Post, PostIndex)
My index.py file:
from algoliasearch_django import AlgoliaIndex
class PostIndex(AlgoliaIndex):
fields = ('title')
settings = {'searchableAttributes': ['title']}
index_name = 'Blog Posts Index'
should_index = 'is_published'
And that should pretty much work! Simple when you know how, or after trying about 10 different options!
So since nobody is answering I tell you how I solved this issue but I have to say that it is not a nice Way and not a "clean" Solution at all. So what I did is went into "taggit managers" in the site-packages (env->lib->python2.x/3.x-> site_packages->taggit->managers.py) In the managers.py file you will find at line 394 this beautiful piece of code:
def __get__(self, instance, model):
if instance is not None and instance.pk is None:
raise ValueError("%s objects need to have a primary key value "
"before you can access their tags." % model.__name__)
manager = self.manager(
through=self.through,
model=model,
instance=instance,
prefetch_cache_name=self.name, # this is the line I comment out when building the index,
name=self.name #this is the line I added and needs to be commented out after the index is build.
)
return manager
So what I do when I want to rebuild the search index is comment out (putting"#" infront of the line) prefetch_cache_name=self.name, and replace it with name=self.name. So building the index will work. After the Index is finished building, you have to bring everything back as it was before (switch the "#" to name=self.name again and leave prefetch_cache_name=self.name, visible again).
As already mentioned this is probably not the best way but I had the same pain and this is working for me. It takes one minute when you have the routine. Since I have to rebuild the Index maybe once every two weeks, that isn't such a deal for me but if you have to do it very often this might be annoying...
Anyway I hope that helps you.
It can help you if you using django==2+
The problem is in get_queryset() method of TaggableManager
Open file with it (my path was: Pipenv(project_name)/lib/site-packages/taggit/manager.py)
Find _TaggableManager class and change method name get_queryset to get_query_set
Done. I wish taggit's developers will fixed this in future updates
I'm trying to implement a list of foreignKeys in django-nonrel (I'm using mongo as db).
Here is the code:
# models.py
from django.db import models
from django_mongodb_engine.contrib import MongoDBManager
from djangotoolbox.fields import ListField
class FriendList(models.Model):
objects = MongoDBManager()
list = ListField(models.ForeignKey('AWUser'))
def add_friend(self, awuser):
# awuser must be an instance of AWUser - I removed tests for more clarity
self.list.append(awuser)
self.save()
class AWUser(models.Model):
objects = CustomUserManager()
user = EmbeddedModelField('User')
friends = EmbeddedModelField('FriendList')
The problem is that when I call user.friends.add_friend(user1), I have the error "AttributeError: 'str' object has no attribute '_meta'".
Breaking example (made using ./manage shell console):
$>user = AWUser.objects.all()[0]
$>user1 = AWUser.objects.all()[1]
$>user.friends.add_friend(user1)
#ask me if you need the complete error - I don't put it more more clarity
AttributeError: 'str' object has no attribute '_meta'
What I basically need is to create friend lists.
Please feel free to recommend a different implementation if you think mine is not good. :) I would love to have my implementation working though...
Also, I did not put all the variables of AWUser for more clarity but I can add them if necessary.
Related project dependencies:
django 1.3.1 (installed as django-nonrel)
django-mongodb-engine 0.4.0
djangotoolbox==0.9.2
List item
pymongo==2.1.1
Thanks for your help.
UPDATE:
I tried to change the code as said in the post "ListField with ForeignField in django-nonrel" but I still have the same error...
According to the Django MongoDB Engine documentation, it suggests to use EmbeddedModel from djangotoolbox:
from djangotoolbox.fields import ListField, EmbeddedModelField
class Post(models.Model):
...
comments = ListField(EmbeddedModelField('Comment'))
class Comment(models.Model):
text = models.TextField()
Edit: Forgot the link: http://django-mongodb-engine.readthedocs.org/en/latest/topics/embedded-models.html
I actually just figured out what was wrong.
It is apparently impossible to declare the foreign key class type as a string when in a Listfield. Weird...
If it happens to you, just do the following change:
list = ListField(models.ForeignKey('AWUser'))
becomes:
list = ListField(models.ForeignKey(AWUser))
If anyone as a good explanation of what is going on, I'd love to hear it :)
I'm trying to find some documentation of how to use the ForeignKeyRawIdWidget in my own forms. Currently I keep getting the error, "init() takes at least 2 non-keyword arguments (1 given)" which tells me nothing.
Any help would be most appreciated. Googling this turns up little but dev conversations and no examples that I can find of how to implement it.
Update: This is solved; see solution below.
As of the Django 1.5, this works to reuse the ForeignKeyRawIdWidget in non-admin forms.
from django.contrib.admin.sites import site
class InvoiceForm(ModelForm):
class Meta:
model = Invoice
widgets = {
'customer': ForeignKeyRawIdWidget(Invoice._meta.get_field('customer').rel, site),
}
Update
Django 2.0 is deprecating field.rel in favor of field.remote_field. You might want to use this instead (also works on Django 1.11):
...
ForeignKeyRawIdWidget(Invoice._meta.get_field('customer').remote_field, site),
...
This is from the source code (django.contrib.admin.widgets):
class ForeignKeyRawIdWidget(forms.TextInput):
"""
A Widget for displaying ForeignKeys in the "raw_id" interface rather than
in a <select> box.
"""
def __init__(self, rel, attrs=None):
self.rel = rel
super(ForeignKeyRawIdWidget, self).__init__(attrs)
#.....
From the remaining code, I would guess that rel is the foreign key field of your model. At one point, the code checks self.rel.limit_choices_to, and this attribute (limit_choices_to) can only be set on a ForgeinKey field.
I've been trying to get syntax highlighting working in my simple Django (1.1) test app using Markdown (2.0.1) & Pygments (1.0). The idea is to generate HTML from the users input which is in markdown format and store both in the DB so I don't have to do the markdown to html translation during the fetch.
So far I have the markdown processing working but I cannot seem to get syntax highlighting working. My models.py looks like this:
from django.db import models
from django.contrib import admin
from markdown import markdown
class BlogPost( models.Model ):
title = models.CharField( max_length = 150 )
body = models.TextField()
body_html = models.TextField(editable=False, blank=True, null=True)
timestamp = models.DateTimeField()
def save(self):
self.body_html = markdown(self.body, ['codehilite'])
super( BlogPost, self).save()
class Meta:
ordering = ( '-timestamp', )
class BlogPostAdmin( admin.ModelAdmin ):
list_display = ( 'title', 'timestamp' )
admin.site.register(BlogPost, BlogPostAdmin)
So far testing just markdown syntax works but if I try something like the following I don't seen any syntax highlighting in the output or the output source:
:::python
from foo import bar
foobar = bar('foo')
I'd expect to see at least a set of code elements in the output source.
Fixed it! The code should have been indented four spaces not three!
I made multiple edits to test that out before asking the question but it would seem Firefox cached the page as using as a test post. As I had been using the windows keyboard shortcut to force a page reload not the mac keyboard shortcut, d'oh!
I spotted it was working when I made a new test post out of frustration with four space indenting and then inspected the page source.
It's better to store it in the database in markdown format, and then convert it to the presentation format you'd like (HTML) at display time. That way you can edit your data the same way you added it in the first place.
At the top of your template you should include:
{% load markup %}
Then use the template filter markdown.
{{ blog_post.body|markdown}}
Then just use css to make sure you have the proper formatting.
You also need to install the markdown package if you don't have it here.
And in your settings.py in your INSTALLED_APPS you should include 'django.contrib.markup'
For more information see this page.
As for why you don't see formatting, check the marked up source and make sure it is working correctly. i.e. make sure it is marking up properly. Then make sure you have the needed stylesheets.
Markdown format is the format before it is marked up.
You can also use JQuery to add a class to the marked up elements, so you can style the markdown text without affecting the rest of the page.