Creating an "Other" Field

Creating an other field

Different package/different syntax version:

library(data.table)

dt = as.data.table(DF)

dt[order(-n), # your data is already sorted, so this does nothing for it
if (.BY[[1]]) .SD else list("Other", sum(n)),
by = 1:nrow(dt) <= 5][, !"nrow", with = F]
# Category n
#1: E 163051
#2: K 127133
#3: M 106680
#4: L 64868
#5: I 49701
#6: Other 217022

How to create a field type based on another field value?

You need a mapped type to iterate over the keys of your Config type and create the pairing.

type ConfigContainer = {
[K in keyof Config]: { component: K, config: Config[K] }
}[keyof Config]

This creates an object type that has all possible pairings as values, and then you index that object by it's own keys to get the value types as a union.

See Playground

SQL Create Field based on Other Field in Record

This is one "solution", there are many more.

You could create one table called "Person" consisting of FirstName, LastName and Hometown (I presume you may have that table already) and a lookup table called "CityToState" with City and State.

Fill the lookup table with appropriate data (it'll be quite large, I'm sure) and issue the query

select FirstName, LastName, Hometown, State 
from Person left join CityToState on Hometown=City;

That should give you the correct data, with NULL returned for the state if the city does not exist in the lookup table.

Basically what this does is to get all data from Person and join it, row by row with the row in CityToState where HomeTown is the same as City. The "left" part means that it should return the row from the left table (Person) even if there is no matching row in CityToState.

How to create a field in django model using values of other fields?

Rather than making a Model Field, you can make a method inside OrderAdmin to generate total in runtime. Then show it in the adminsite. Like this:

from django.db.models import Sum, F, ExpressionWrapper, DecimalField

class OrderAdmin(admin.ModelAdmin):
list_display = ['id','name','order_total' ,'mobile_no','address','status', 'created']
list_editable = ['status']
list_per_page = 10
list_filter = ['status']
readonly_fields= ['order_total']
search_fields = ('id','mobile_no','name',)
inlines = [OrderItemInline]

def order_total(self, obj):
return obj.items.annotate(price_per_item=ExpressionWrapper(F('quantity')*F('price'), output_field=DecimalField())).aggregate(sum_total=Sum('price_per_item')).get('sum_total')

In total_order method in OrderAdmin, I am calculating the total of that order using aggregation. Then I have added the total_order to both fields and readonly_fields in OrderAdmin.

How to make one field Dependent on other field's value in meteor autoform?

You can use getFieldValue to get a particular field's value, and based on it return a set of option or defaultValue from the template helper.
Documentation is found here

So in accordance with your code:

form.html:

    ...
{{#autoForm id="sampleFormID" ... ... }}
{{> afQuickField name='Setup' value=type readOnly=true}}
{{> afQuickField name='count' options=allowedOptionHelper defaultValue=defaultValueHelper }}
...

{{/autoForm}}

form.js

Template.templateName.helpers({

allowedOptionsHelper: function() {
if (AutoForm.getFieldValue('Setup', 'sampleFormID') === 'A') {
return [{label:"9", value:"9"},{label:"11",value:"11"},{label:"12", value:"12"}];
else
if (AutoForm.getFieldValue('Setup', 'sampleFormID') === 'B') {
// return the options for B
} else if (AutoForm.getFieldValue('Setup', 'sampleFormID') === 'C')) {
// return the options for C
}
},

defaultValueHelper: function() {
if (AutoForm.getFieldValue('Setup', 'sampleFormID') === 'A') {
return 11 //or whatever the defaultValue is;
}
//likewise for options B and C
}

});

schema.js

...
...
count:{
type: String,
label:"No. Of count",
optional: true,
autoform:{
afFieldInput:{
firstOption:"(Select the count)"
}
}
}
...
...

How do i create a field in my model whose value is calculated using values from other fields?

You don't need to create additional field for score instead you can make score property of your model

class TradeBill(models.Model):
...
@property
def score(self):
return self.first + self.second + self.third
...

EDIT
Extending the answer to include a serializer.
You can also include the property of the model in the serializer

class TradeBillSerializer(serializers.ModelSerializer):
class Meta:
fields = ("score", ...)

Also, you can use SerializerMethodField (as mentioned by @Saif eldeen Adel in the comments.

class TradeBillSerializer(serializers.ModelSerializer):

score = serializers.SerializerMethodField()

def get_score(self, obj):
return obj.score

class Meta:
fields = ("score", ...)

SerializerMethodField provides the flexibility to perform adding processing or formatting to the field.

Create a field with conditional values based on other fields in SQL

If you create 3 queries and join the with union all you will have your result.

SELECT CONCAT(ID,'_Tom') ID2
Revenue_Tom Revenue
FROM table_name
WHERE Revenue_Tom > 0
UNION ALL
SELECT CONCAT(ID,'_John') ID2
Revenue_John
FROM table_name
WHERE Revenue_John > 0
UNION ALL
SELECT CONCAT(ID,'_Lisa') ID2
Revenue_Lisa
FROM table_name
WHERE Revenue_Lisa > 0;

SQL Server - create value one field from other field in same View table

You only have value of the current year available in each row. And your calculation needs access to the value of the previous year.

You could fetch the values of the previous year with a join like FiscalYear-1

Now you have both the current (B_1) and it's previous (B_2) year available on row level and you can use it in your calculation.

SELECT  
*
FROM dbo.DataLeaveBalance AS A
LEFT OUTER JOIN dbo.VWLeave_takens AS B_1
ON A.FiscalYear = B_1.AffectFY
AND A.EmpCode = B_1.EmpCode
LEFT OUTER JOIN dbo.VWLeave_takens AS B_2
ON (A.FiscalYear-1) = B_1.AffectFY
AND A.EmpCode = B_1.EmpCode
LEFT OUTER JOIN dbo.MasterEmployee AS C
ON A.EmpCode = C.EmpCode

create a calculated field from other model's field

The relevant documentation is: https://docs.djangoproject.com/en/1.11/topics/db/aggregation/

from django.db.models import Avg

students = Student.objects.annotate(Avg('subject__point'))


Related Topics



Leave a reply



Submit