Setting default value for Foreign Key attribute

I would modify @vault’s answer above slightly (this may be a new feature). It is definitely desirable to refer to the field by a natural name. However instead of overriding the Manager I would simply use the to_field param of ForeignKey: class Country(models.Model): sigla = models.CharField(max_length=5, unique=True) def __unicode__(self): return u’%s’ % self.sigla class City(models.Model): … Read more

Java casting order

(C)a.foo() is equivalent to (C)(a.foo()), i.e. #2 in the question. To get #1, you would have to write ((C)a).foo(). The Java language specification does not specify operator precedence in a nice, easy-to-read summary. Appendix A of Introduction to Programming in Java by Sedgewick and Wayne has a comprehensive table of operator precedence. Appendix B of … Read more

How to change Android O / Oreo / api 26 app language

I had the same problem: since Android 8.0+ some parts of my app did’t change their language anymore. Updating of both application and activity context helps me. Here is an example of MainActivity function: private void setApplicationLanguage(String newLanguage) { Resources activityRes = getResources(); Configuration activityConf = activityRes.getConfiguration(); Locale newLocale = new Locale(newLanguage); activityConf.setLocale(newLocale); activityRes.updateConfiguration(activityConf, activityRes.getDisplayMetrics()); … Read more

What is the purpose of the default keyword in Java?

It’s a new feature in Java 8 which allows an interface to provide an implementation. Described in Java 8 JLS-13.5.6. Interface Method Declarations which reads (in part) Adding a default method, or changing a method from abstract to default, does not break compatibility with pre-existing binaries, but may cause an IncompatibleClassChangeError if a pre-existing binary … Read more

Why Is `Export Default Const` invalid?

const is like let, it is a LexicalDeclaration (VariableStatement, Declaration) used to define an identifier in your block. You are trying to mix this with the default keyword, which expects a HoistableDeclaration, ClassDeclaration or AssignmentExpression to follow it. Therefore it is a SyntaxError. If you want to const something you need to provide the identifier … Read more

Default value of a type at Runtime [duplicate]

There’s really only two possibilities: null for reference types and new myType() for value types (which corresponds to 0 for int, float, etc) So you really only need to account for two cases: object GetDefaultValue(Type t) { if (t.IsValueType) return Activator.CreateInstance(t); return null; } (Because value types always have a default constructor, that call to … Read more