Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 months ago.
Improve this question
Can you please provide an example of views.py for updating(or creating) fields without using forms.py?
Here you go.
Please, next time, do some research.
Just searching "django update or create" will give you a very nice django doc to the queryset method.
obj, created = Person.objects.update_or_create(
first_name='John', last_name='Lennon',
defaults={'first_name': 'Bob'},
)
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to customize Sales 365. I need a field in a Lead form to multiselect items from Account View.
Is there any way to do it without coding?
Thanks
You can't have a field but you can have a subgrid, relationship between Lead and Account will be N:N, in this way the user can select multiple records from the Account entity.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a JSONField which seems to be successfully storing a JSON as a string in my database.
How do I retrieve this data as a dictionary?
class Package:
node = JSONField(null=True, blank=True)
packageInstance = Package.objects.get(id=packageId)
print(packageInstance.node)
Your packageInstance.node is a python dictionary already
The official docs verify it too https://docs.djangoproject.com/en/2.0/ref/contrib/postgres/fields/#jsonfield
Simply put Django converts the json string to python dict automatically when you work with JSONField like you showed.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Question about DRF serializers. Especially ModelSerializer.
There are two versions with Foreign Key:
field = CharField(source='user.name')
field = SeriaizerMethodField()
get_field(self, obj):
return obj.user.name
What will work better / quickly ?
UPD
yes, I know about select/prefetch_related.
But is better just to pull required field from database and serialized it automaticaly or pull some 'raw' fields and nicely join them in serializer?
There are no difference between them. If you want to improve speed you should do it in a view by calling select_related method of a queryset.
YourModel.objects.select_related('user')
It'll join user table and calling user.name atribute will not hit a database next time
First one provides different validation options from the box. While the second one is something very customizable and doesn't provide any validation itself.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I show list of available things in database against a search. Now i want to make each item fetched by database a link which can lead to another page and on that page i could show this chosen item in detail. So my problem is how can i pass this information from this page to the new page?
You can pass the id of your item, so in the new page you can query again the database for more details.
if you use include tag you can pass object also:
{% include "url/to/your/template" with object=your_object %}
and then inside your detail's template you can call as usual
{{ your_object.detail }}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I want escape html for data in controller, I found 2 ways:
text.gsub('<', '<').gsub('>', '>')
OR
CGI.escapeHTML(text)
What is the best, why?
The best way is probably to just assign the data to instance variables and then output them in the view, which will handle encoding automatically:
controller:
def something
#foo = "<test>"
end
something.html.erb
<%= #foo %>
Will output:
<test>
The view will do the right thing in most cases.