I have two datepickers for a date field. One truncated (flask_admin, I believe) and another is because the form_widget_args, as bellow:
form_widget_args = {
'date': {
'type': 'date',
'rows': 20
}
}
How can I eliminate or fix the truncated one?
Solved.
form_widget_args = {
'date': {
'type': 'date',
'autocomplete': 'off',
'data-role': '',
'rows': 20
}
Related
data = [
{
'name': 'Instagram',
'follower_count': 346,
'description': 'Social media platform',
'country': 'United States'
},
{
'name': 'Cristiano Ronaldo',
'follower_count': 215,
'description': 'Footballer',
'country': 'Portugal'
},
{
'name': 'Ariana Grande',
'follower_count': 183,
'description': 'Musician and actress',
'country': 'United States'
}]
def dictionary_value():
for value in data:
return value["name"], value["follower_count"],value["description"], value["country"]
Hi, I'm newbie in python and i have a question about dictionary and lists:
i would like that my function dictionary value() will return only the values and print the function by the location in the list, for example: if i want to choose data[2] the outcome should be: 'Ariana Grande', 183,'Musician and actress','United States'. i could not find a way to do it.
Use dict.values method:
def dictionary_value(i):
return list(data[i].values())
Output:
print(dictionary_value(2))
# 'Ariana Grande', 183,'Musician and actress','United States'
I have this code:
initial_data = dict(request.data.copy())
reserve_data = dict(request.data.copy())
print(initial_data)
for key in initial_data.keys():
merchant_data = initial_data.get(key)
for sub_key in merchant_data.keys():
if sub_key in keys_to_change:
reserve_data[key].pop(sub_key)
reserve_data[key][values_to_change.get(sub_key)] = merchant_data.get(sub_key)
print(initial_data)
As you can see, I am not changing initial_data, but it changes anyway
#before
{'22': {'domain': 'cashier.random.io', 'salt': 'ewrwerwe', 'active': 1, 'separate_cashier': '', 'additional_hosts': {}, 'tradingroom_url': '', 'crm': {'login': '', 'secret': '', 'url': ''}, 'currencies': ['USD', 'EUR'], 'payment_methods': {'12': {}}, 'processors': {}}}
#after
{'22': {'salt': 'ewrwerwe', 'separate_cashier': '', 'additional_hosts': {}, 'tradingroom_url': '', 'crm': {'login': '', 'secret':
'', 'url': ''}, 'currencies': ['USD', 'EUR'], 'payment_methods': {'12': {}}, 'processors': {}, 'host': None, 'is_active': None}}
Is there a way to avoid this? Thanks everybody
initial_data = dict(request.data.copy())
reserve_data = dict(request.data.copy())
does shallow copies of the request data.
If you're internally modifying the contents, you'll need to use copy.deepcopy instead:
initial_data = dict(copy.deepcopy(request.data))
reserve_data = dict(copy.deepcopy(request.data))
I am trying to auto-populate one field of a Django form based on the query response of an entry into another field. I would like to do so before the form is submitted, displaying the autopopulated value in the field and maintain the user's ability to edit.
(Example, I type 'a handful of bananas' into the name field, when that field blurs it queries the api and fills the carbs field with the # of carbs a handful of bananas has)
Right now I have the ajax function to query:
$('#recipe-query').on('blur', function(event){
event.preventDefault();
console.log("blur")
query_macros();
});
function query_macros(){
var dataStr = "APIKEYREDACTED"
var ingredParam = encodeURI($('#recipe-query').val())
dataStr = dataStr + ingredParam
if($('#recipe-query').val() && $('#should-query').val()){
$.ajax({
url: "https://api.edamam.com/api/nutrition-data",
data: dataStr,
type: "GET",
dataType: "json",
success : function(json){
console.log(json);
console.log(json.totalNutrients.CHOCDF.quantity)
$('#carbs-query').value = json.totalNutrients.CHOCDF.quantity;
},
error : function(xhr, errmsg, err){
console.log(xhr.status + ": " + xhr.responseText);
}
});
}
}
this is the form
class NutritionForm(forms.ModelForm):
class Meta:
model = Nutrition
fields = ('food_name', 'autofill_macros', 'grams_of_fat',
'grams_of_protein', 'grams_of_carbs', 'date')
widgets = {
'food_name': forms.TextInput(attrs={
'id': 'recipe-query'
}),
'autofill_macros': forms.CheckboxInput(attrs={
'id': 'should-query'
}),
'grams_of_fat': forms.TextInput(attrs={
'id': 'fat-query'
}),
'grams_of_protein': forms.TextInput(attrs={
'id': 'protein-query'
}),
'grams_of_carbs': forms.TextInput(attrs={
'id': 'carbs-query'
})
}
and model
class Nutrition(models.Model):
food_name = models.CharField(max_length=50)
autofill_macros = models.BooleanField()
grams_of_protein = models.PositiveSmallIntegerField()
grams_of_carbs = models.PositiveSmallIntegerField()
grams_of_fat = models.PositiveSmallIntegerField()
calories = models.PositiveSmallIntegerField()
date = models.DateField()
user = models.ForeignKey(User, on_delete=models.CASCADE)
but the #carbs-query field is not updated. How do I use the result of the API call to populate the other field while maintaining its editability (all before form submit).
It seems like js code have some issues. you can try below method of jQuery to set value:
$('#carbs-query').val(json.totalNutrients.CHOCDF.quantity);
Full code:
function query_macros(){
var dataStr = "APIKEYREDACTED"
var ingredParam = encodeURI($('#recipe-query').val())
dataStr = dataStr + ingredParam
if($('#recipe-query').val() && $('#should-query').val()){
$.ajax({
url: "https://api.edamam.com/api/nutrition-data",
data: dataStr,
type: "GET",
dataType: "json",
success : function(json){
console.log(json);
console.log(json.totalNutrients.CHOCDF.quantity)
$('#carbs-query').val(json.totalNutrients.CHOCDF.quantity);
},
error : function(xhr, errmsg, err){
console.log(xhr.status + ": " + xhr.responseText);
}
});
}
}
Django 1.10.
https://docs.djangoproject.com/en/1.10/topics/forms/formsets/#can-order
https://docs.djangoproject.com/en/1.10/topics/forms/formsets/#can-delete
The examples at both of the abovementioned links are as follows:
>>> data = {
... 'form-TOTAL_FORMS': '3',
... 'form-INITIAL_FORMS': '2',
... 'form-MAX_NUM_FORMS': '',
... 'form-0-title': 'Article #1',
... 'form-0-pub_date': '2008-05-10',
... 'form-0-DELETE': 'on',
... 'form-1-title': 'Article #2',
... 'form-1-pub_date': '2008-05-11',
... 'form-1-DELETE': '',
... 'form-2-title': '',
... 'form-2-pub_date': '',
... 'form-2-DELETE': '',
... }
>>> formset = ArticleFormSet(data, initial=[
... {'title': 'Article #1', 'pub_date': datetime.date(2008, 5, 10)},
... {'title': 'Article #2', 'pub_date': datetime.date(2008, 5, 11)},
... ])
What troubles me: why should we need initial data here? It overburdens the documentation as we don't use the initial data in the example. The only case when we need both data and initial is when we use has_changed().
I'm a newbie. Maybe I don't understand that deep.
How do you think, maybe I should draw the attention of the community at Djangoproject to this problem?
I have a object, which has multiple objects
fields:[
{
safeName: 'status',
accessorName: 'Status',
type: {name: 'String', doc: 'String', code: '\'String\''},
field: {type: 'string', description: 'pet status in the store', enum: ['available', 'pending', 'sold']}
},
{
safeName: 'code',
accessorName: 'Code',
type: {name: 'NSInteger', doc: 'NSInteger', code: '\'NSInteger\'' },
field: {type: 'integer', format: 'int32'}
},
...
]
I need to check with enum value
Output should be
When enum is present in field
instance.status = Order.Status(rawValue: (sourceDictionary["status"] as? String) ?? "")
And when enum is not present in field object
instance.code = Decoders.decodeOptional(clazz: NSInteger.self, source: sourceDictionary["code"])
Did like this
{{#fields}}{{#field}}
{{#description}}
instance.{{safeName}} = Order.Status(rawValue: (sourceDictionary["{{safeName}}"] as? String) ?? "")
{{/description}}
{{^description}}
instance.{{safeName}} = Decoders.decodeOptional(clazz: {{#type}}{{type.name}}{{/type}}.self, source: sourceDictionary["code"])
{{/description}
{{/field}}{{/fields}}