Custom index name in South/Django - django

Is there a way to set our own index name in Django models? Currently, the migration scripts would create a name in format [table_name]_9fcb4ba3 and I'd like to have the name more descriptive, i.e. [table_name]_[column_name] or so.
On the Django's Model Field Reference page, it doesn't seem to have such option (https://docs.djangoproject.com/en/1.8/ref/models/fields/#db-index)

For anyone finding its way from Google - seems Django 1.11 allows you now to have a custom name for indexes. Following the documentation as described here:
Index.name
The name of the index. If name isn’t provided Django will auto-generate a name. For compatibility with different databases, index names cannot be longer than 30 characters and shouldn’t start with a number (0-9) or underscore (_).

There is no way of customizing the name for indexes as these are generated by hashing (the index name calculation uses some hashing techniques)

Related

Django-dsl-drf Exclude phrase query

I am working on integrating Elastic Search in my existing Django REST application. I am using the django-dsl-drf module provided in the link below:
https://django-elasticsearch-dsl-drf.readthedocs.io/
In their documentation 'exclude' query param is provided. But the query only when we provide the full field value.
search-url?exclude=<field-value
For eg: If I have a value 'Stackoverflow' in field 'name'. I'll have to provide query param a
?name__exclude=Stackoverflow to exclude records having 'Stackoverflow' as name in the result. I would like to implement a search in such a way that when I provide 'over', I need to exclude these records, similar to ?name__exclude=over
I checked the above tutorial, but I couldn't find it. Is there any work around so that I can exclude records, fields containing terms instead of providing full field value, which is also case-insensitive.
Thanks a lot.
Using the contains functional filter, you can target documents that have their name field value containing the characters over anywhere in their terms:
?name__contains=over
However, as far as I know, there is no way to negate that filter in django-dsl-drf. You can create an issue requesting that feature, though, because odds are high that you're not the only who needs that, since it's a pretty common way of searching.

How to solve Sitecore Glass Mapper duplicate field names

I've a template with sections below:
Section A
Title
Name
Section B
Title
Name
Looks good on Sitecore. However when TDS Glass mapper generates the code, it generates Title and name twice.
Wondering if it's possible to add the section name before each field when glass mapper generates the code.
If you ask me why, it's to avoid 248 character length error for windows file system for TDS items as I had done this as work around.
Section A
Section A Title
Section A Name
Section B
Section B Title
Section B Name
Any help will be awesome.
Thanks.
It is generally not good practice to have Fields with the same name defined multiple times in the same template. Although Sitecore will allow you to do that and will be able to identify the fields separately by the Field ID - Most code uses field names or generated code to do that. When referencing the fields by name, it will fail.
You have 2 options.
Option 1
It is better practice to either prefix the names or come up with a unique naming convention, as you have done in your example
Option 2
As an alternative you could modify the T4 template to prefix the field names when generating the code, with the section name. This would allow the code to compile. But depending on how the fields are being mapped by Glass (normally this is by field name, not ID) - it will still cause issues as Sitecore will not know which field to use.
I would go with Option 1

Django syncdb doesn't work after invoking inspectdb

I created a model from an existing PostgreSql database using inspectdb, when I try to do a syncdb,in order to generate the authorization tables, it generates the following error:
CommandError: One or more models did not validate:
db.t1: "ip": CharFields require a "max_length" attribute that is a positive integer.
So I put the max_length=255 to all CharFields but it doesn't work neither with that. Django version is 1.5.1.
Anyone have an idea how to fix this?
Currently inspectdb doesn't set max_length for PostgreSQL char fields without specified length. FYI, quote from postgreSQL docs:
The notations varchar(n) and char(n) are aliases for character
varying(n) and character(n), respectively. character without length
specifier is equivalent to character(1). If character varying is used
without length specifier, the type accepts strings of any size. The
latter is a PostgreSQL extension.
But, Django doesn't allow to define CharFields without max_length parameter. There is an open ticket for it.
This django snippet provides a custom CharField without length limit - should pass all django's validation.
Also, switching to TextField could help too.
Hope that helps.

Create/Edit MS Word & Word Perfect docs in Django?

Is it possible to create and/or edit MS Word and Word Perfect documents with django? I'd like to be able to allow the user to fill out a form and have the form fields inserted into an MS Word/Word Perfect document. Or, the form fields are used to create a new MS Word/Word Perfect document. The user can then send that document via email to others who may not have access to the django web-app.
I have a client who needs this functionality and I'd like to keep it all within the web-app.
Any ideas?
Thanks!
For MS Word, you could use docx-mailmerge. Run the following commands to install lxml(dependecy required by docx-mailmerge) and docx-mailmerge
conda install lxml
pip install docx-mailmerge
In order for docx-mailmerge to work correctly, you need to create a standard Word document and define the appropriate merge fields. The examples below are for Word 2010. Other versions of Word should be similar. It actually took me a while to figure out this process but once you do it a couple of times, it is pretty simple.
Start Word and create the basic document structure. Then place the cursor in the location where the merged data should be inserted and choose Insert -> Quick Parts -> Field..:
Word Quick Parts
From the Field dialog box, select the “MergeField” option from the Field Names list. In the Field Name, enter the name you want for the field. In this case, we are using Business Name.
Word Add Field
Once you click ok, you should see something like this: <> in the Word document. You can go ahead and create the document with all the needed fields.
from __future__ import print_function
from mailmerge import MailMerge
from datetime import date
template = "Practical-Business-Python.docx"
document = MailMerge(template)
document.merge(
status='Gold',
city='Springfield',
phone_number='800-555-5555',
Business='Cool Shoes',
zip='55555',
purchases='$500,000',
shipping_limit='$500',
state='MO',
address='1234 Main Street',
date='{:%d-%b-%Y}'.format(date.today()),
discount='5%',
recipient='Mr. Jones')
document.write('test-output.docx')
More at http://pbpython.com/python-word-template.html
I do not know how to do exactly what you ask for, but I would suggest that you also look into creating PDFs with Django. If you only want to send information in a particular format then PFD might be better because it is more portable across platforms. You may also want to look at this documentation for how to send emails from Django.

Django: List of valid field lookup operators

Does Django have an accessible list of all valid field lookup operators (those that are used by the QuerySet API, e.g. 'contains', 'in', 'lt', etc)?
Thanks
EDIT: For clarification, I mean an in-code list that I can import so, for example, I can check if a given string matches a valid operator.
After searching the source for the operators, it lives in django.db.models.sql.constants.QUERY_TERMS.
A dictionary with lookup strings mapped to None.
'exact' in QUERY_TERMS
Thanks for this! Never would have gone looking, but I could definitely use this.
As of Django 2.1, django.db.models.sql.constants.QUERY_TERMS constant is removed. Per the release notes, Django recommends the get_lookups() method of the Lookup Registration API as an alternative.
For a given field on a model, it can be accessed via:
MyModel._meta.get_field('my_field').get_lookups()
they are in
your_model._meta.fields[index].class_lookups
It’s a dict.