I have a frontend that will be sending bas64 images, I will put those very large strings on a variable and send it to a form. What kind of FormField can I use?
The regular CharFields need a max_length. Since it is a very large string, I don't know the length it could have.
Maybe TextField could be the right choice.
Related
With a model Book that uses MarkDown for a field called content, when I do the following query
Book.objects.filter(published=True).order_by('read')
The site becomes slow because of the content field, I think the hard work occurs when Django tries to convert these fields to python object. When I clean all the content field for each record and leave them blank, the query is pretty much faster.
In my case content field contains large text. To gain in performance, How can I fetch data by ignoring a specific field?
I want to ignore content field like:
Book.objects.filter(published=True)#.ignore_fields('content',).order_by('read')
Try to use defer:
Book.objects.defer('content').filter(published=True).order_by('read')
I have a ModelForm with some IntegerFields and DecimalFields, and I need to render them on server using current locale format for view, and using regular format for JS interop.
Currently I'm using form.currency.value|default:1|stringformat:"d" for integers and form.rate.value|default:0|stringformat:"f" for decimals.
But since that's form, when there's bound data in form, form.rate.value is string, and stringformat returns empty string.
Is it possible to access normalized data from template? If yes - how? If no - how should I handle both cases? Handling this in view for each and every field seems like an overkill, but handling types in template seems no better.
Best way I can think of is to register custom filter, handle types there, return string with number literal.
I am getting data from my database and one of the fields could contain a large amount of text and I want to limit the amount of text it returns. Basically I am looking for the equivalent to PHP's substr function.
Right now I am grabbing the content from my database by
{{post.First_Name}} {{post.Last_Name}}
Is there a way I can limit the amount of text it returns from this field?
Thanks.
You may wish to use truncatewords filter. You do it like so:
{{ post.First_Name|truncatewords:3 }}
You can gain even more precision if you want to. You can learn more about filters, and this one is particular here. You can gain even more fine-grain control with truncatechars if you so wish.
In my models I need to store a mobile number in the following format 447182716281. What field should I use? Does Django have anything to support this?
example
mobile = models.IntegerField(max_length=12)
Phone numbers must be CharFields. Integer field will not preserve leading 0, +, and spacing.
I think is a interesting question since it really depends on the problem modeling, CharField works fine, but have a look at this:
ORM tricks
There is a regex field in form validation. In model use just CharField.
On using models.IntegerField(default=0)
for larger number it gives error
Ensure this value is less than or equal to 2147483647.
So better way to use would be.
BigIntegerField
A 64-bit integer, much like an IntegerField except that it is guaranteed to fit numbers from -9223372036854775808 to 9223372036854775807. The default form widget for this field is a TextInput.
with PostgreSQL IntegerField is not working properly so it's better to use CharField with Django.
Number=models.CharField(max_length=12)
I have a field in a model that I want users to feel like they can write an arbitrary amount of text in. Django provides a CharField and a TextField in the models. I assume that the difference is that one of them is a char(max_length) and the other is a varchar internally.
I am tempted to use the TextField, but since it doesn't respect max_length, I am somewhat wary of someone dumping loads of data into it and DOSing my server. How should I deal with this?
Thanks!
Fields in model only represent the way data is stored in database.
You can very easily enforce maximum length in form which will validate users' input. Like this.
class InputForm(forms.Form):
text = forms.CharField(max_length=16384, widget=forms.TextArea)
...
This will make sure the maximum length user can successfully enter is 16k.