I'm building a new bible website, using Django.
I started with English and now I want to translate it to other languages like Spanish, Hebrew, etc.
Here's the issue:
The bible has many versions - King James bible, new international bible, etc.
Each version can be in a few languages - like English, Spanish, etc.
Here's the model I used for the default King James english bible:
class verses (models.Model):
id = models.IntegerField("ID",primary_key=True)
book = models.CharField("Book",max_length=20,default=1)
chapter = models.IntegerField("Chapter",default=1)
verse = models.IntegerField("Verse",default=1)
versetext = models.CharField("Versetext",max_length=550,default=1)
title = models.CharField("Title",max_length=100,default=1)
summary = models.CharField("Summary",max_length=550,default=1)
id: a primary key for each verse book - the name of the book (in
English). For example: Genesis
chapter: the number of the chapter. For example: 1
verse: the number of the verse. For example: 2
versetext: the verse text. For example: In the beginning God created the heaven and the earth
title: the title of the chapter (in English). For example: Creation
summary: the summary of the chapter (in English). For example: In chapter 1 God creates the heaven and the earth...
I want to achieve two things:
A user should see a default language of the website, based on his
location. Spain users should see the website in Spanish.
A user can choose a version of the bible.
Which changes should I add to my code in order to achieve this?
Should I create a new model for Spanish? or should I put it in the same model with a new column?
Should I create a template per text alignment? RTL vs. LTR?
Related
I'm not entirely sure about the correctness of the question. In fact, I doubt whether I can express exactly what I am looking for.
Question: How can I create additional fields based on the previous selection before submitting the form?
Let's take the number of pets and the name of the pet in our example. I want how many pet names to be entered first
class UrlForm(forms.Form):
INT_CHOICES = [(x, x) for x in range(11)]
number_of_pets = forms.ChoiceField(choices=INT_CHOICES)
and then create as many Charfield fields as the selected value based on the previous selection:
#new fields as many as the number of pets
pet1 = forms.CharField()
pet2 = forms.CharField()
Of course, the variable name issue is a separate issue.
Question 2: As an alternative approach, can I create a pet name field like a tag field? As in the image:
Image taken from here. The question is appropriate but a ReactJs topic.
You can share the topic, title, term, article, anything that I need to look at. Thanks in advance.
Edit:
The pet example is purely fictional. Influenced by the image, I used this topic.
Take books and authors models for example with books having one or more authors. Books having cover_type and authors having country as origin.
How can I list all the books with hard cover, and authors only if they're from from france?
Books.objects.filter(cover_type='hard', authors__origin='france')
This query doesnt retrieve books with hard cover but no french author.
I want all the books with hard cover, this is predicate #1.
And if their authors are from France, I want them annotated, otherwise authors field may be empty or 'None'.
e.g.:
`
Bookname, covertype, origin
The Trial, hardcover, none
Madam Bovary, hardcover, France
`
Tried many options, annotate, Q, value, subquery, when, case, exists but could come up with a solution.
With sql this is so easy:
select * from books b left join authors a on a.bookref=b.id and a.origin=france where b.covertype='hard'
(my models are not books and authors, i picked them because they're django-docs' example models. my models are building and buildingtype, where i want building.id=454523 with buildigtype where buildingtype is active, buildingtype might be null for the building or only 1 active and zero or more passive)
You should use Book id in Auther table.then your query will be like this: Author.objects.filter(origin="france",book__cover_type="hard")
I think i solved it with subquery, outerref, exists, case, when, charfield...too many imports for a simple sql.
`
author = Authors.objects.filter(bookref=OuterRef('id'), origin='France').values('origin')
books = Books.objects.filter(cover_type='hard').annotate(author=Case(When(Exists(author), then=Subquery(author)), default='none', output_field=CharField())).distinct().values('name','cover_type','author')
`
I have two entities, Books and Authors with a strict one-to-many relationship (many-to-many relationship not required for my use case)
The access patterns I want to satisfy are:
Get Author Info by Author Name
Get Book Info By just ISBN
Get all Books records by an Author using Author Name.
Do I need any GSI given the constraint that I can make only a single request to DB when adding a Book or an Author, and fulfill above three access patterns also with a single request?
If my Author Entity uses this key schema:
Partition Key: AUTHOR#XYZ
Sort Key: AUTHOR#XYZ
and for Book Entity I use
Partition Key: BOOK#123
Sort Key BOOK#123
I can get author info by name and book info by ISBN easily. How do I get the 3rd access pattern, entire book data by author name?
Two approaches I thought of:
Have a third entity in the table with PK AUTHOR#XYZ, SK BOOK#123, and use BEGINS_WITH(SK, 'BOOK') but in this approach, when adding a book to DB, I will have to write two items, PK BOOK#, SK BOOK# for getting book by just ISBN and PK AUTHOR#, SK BOOK# for getting all books by author, and the book info will be duplicated in both items.
Add an attribute GSIAuthorName to Book entity when adding a book, and create a GSI with PK GSIAuthorName (AUTHOR#XYZ) and SK being PK of Book entity (BOOK#123). But in this the issue is, in projections I will have to select ALL, since I want all book info attributes by author name, and need to fetch in single query to the GSI, so entire Book Entity will be duplicated in this GSI.
Is there an easier way to model this data?
Since you're trying to have two different access patterns for a single entity that require a different partition key value, there is basically only the two options you have identified correctly.
Your design seems to only work for books that have a single author. In the real world that's not sufficient. There are plenty of books with multiple authors such as "The Dictator's handbook" by Bruce Bueno de Mesquita and Alastair Smith - your data model might want to account for that. Author <-> Book isn't One-to-Many, it's Many-to-Many.
I'd go for something like this which uses a Global Secondary Index. It's very close to your second suggestion.
PK
SK
GSI1PK
GSI1SK
type
attributes
AUTHOR#ALASTAIR SMITH
AUTHOR#ALASTAIR SMITH
author
name, birthdate, ...
AUTHOR#BRUCE BUENO DE MESQUITA
AUTHOR#BRUCE BUENO DE MESQUITA
author
name, birthdate, ...
BOOK#978-1610391849
AUTHOR#ALASTAIR SMITH
AUTHOR#ALASTAIR SMITH
BOOK#978-1610391849
book
title, publisher, author,...
BOOK#978-1610391849
AUTHOR#BRUCE BUENO DE MESQUITA
AUTHOR#BRUCE BUENO DE MESQUITA
BOOK#978-1610391849
book
title, publisher, author,...
Does this introduce data duplication? - Yes
Does this introduce complexity on writes? - Yes
Does it work in the real world? - Yes
The model I've chose allows you to fulfill the requirements:
Get Author Info by Author Name: GetItem on the primary index with PK=AUTHOR#... and SK=Author#...
Get Book Info by just ISBN: Query on primary index with PK=BOOK#... and limit 1
Get all books for an Author: Query on GSI1 with PK=AUTHOR#
When you write a book, you need to add a book record for each author and potentially the author entries. For updates on a books info (which should be very rare) you first do the query as in 2) without the limit and then update each item that comes back.
Update
To address the requests for clarification in the comments:
If you require a strict One-to-Many relationship, I'd pick the second approach
Frequent writes are typically not a problem in your one-to-many case as long as you don't exceed the write throughput of a single partition, which is unlikely given the data. I don't see why you'd need frequent writes though.
The extra complexity is typically only a one-time penalty when you create your data access layer. The code for update_book_by_isbn will have to include the steps I outlined above and the create_book might store multiple records.
This question already has answers here:
What is a "slug" in Django?
(13 answers)
Closed 5 years ago.
In Django generic views, there's slug_field, and slug_url_kwarg.
In this context, what is the definition of slug?
I choose the more persuasive explanation within items of 3 dictionaries.
In Cambridge dictionary:
A piece of metal used instead of a coin for putting in machines
In MW:
A disk for insertion in a slot machine; especially :one used illegally instead of a coin
In Oxford:
A part of a URL which identifies a particular page on a website in a form readable by users.
They don't seem to make sense.
It is from the publishing world from wikipedia:
In newspaper editing, a slug is a short name given to an article that
is in production. The story is labeled with its slug as it makes its
way from the reporter through the editorial process. The AP Stylebook
prescribes its use by wire reporters (in a "keyword slugline") as
follows: "The keyword or slug (sometimes more than one word) clearly
indicates the content of the story."[1] Sometimes a slug also contains
code information that tells editors specific information about the
story — for example, the letters "AM" at the beginning of a slug on a
wire story tell editors that the story is meant for morning papers,
while the letters "CX" indicate that the story is a correction to an
earlier story.[2][3] In the production process of print
advertisements, a slug or slug line, refers to the "name" of a
particular advertisement. Advertisements usually have several markers,
ad numbers or job numbers and slug lines. Usually the slug references
the offer or headline and is used to differentiate between different
ad runs.
From there, the slug for web publishing was born as an effort to make more semantic URLs. This is the slug as used in django:
Some systems define a slug as the part of a URL that identifies a page
in human-readable keywords.[4][5] It is usually the end part of the
URL, which can be interpreted as the name of the resource, similar to
the basename in a filename or the title of a page. The name is based
on the use of the word slug in the news media to indicate a short name
given to an article for internal use. Slugs are typically generated
automatically from a page title but can also be entered or altered
manually, so that while the page title remains designed for display
and human readability, its slug may be optimized for brevity or for
consumption by search engines. Long page titles may also be truncated
to keep the final URL to a reasonable length. Slugs are generally
entirely lowercase, with accented characters replaced by letters from
the English alphabet and whitespace characters replaced by a dash or
an underscore to avoid being encoded. Punctuation marks are generally
removed, and some also remove short, common words such as
conjunctions. For example:
Original title: This, That and the Other! An Outré Collection
Generated slug: this-that-other-outre-collection
Django provides a slug field, and in its documentation provides a definition as well:
Slug is a newspaper term. A slug is a short label for something,
containing only letters, numbers, underscores or hyphens. They’re
generally used in URLs.
I have a model:
class A(models.Model):
name = models.CharField(max_length=120)
name can be written in two languages: English and Russian. When i use A.objects.order_by('-name'), English words stand before Russian. How can i swap it?
This was asked almost three years ago, but if you would like to swap the order, you could simply do:
query = A.objects.order_by("name")
Django changes the order by ascending and descending based on the presence of a - symbol before the field name.