What's the best way to represent many booleans in a django model? - django

I have a model which represents the user's display preferences. Just about all of these preferences are boolean values. Instead of having 50 boolean columns, is there a better way to go about this? In the future when I want to add a new item, I don't want to have to add a new column in my database.

If you have that many booleans and are anticipating adding more, you should not be using columns, but entries.
Then when you need to look up "User wants emails", just search for UserPrefs.objects.get(User=user, Preference=Preferences.objects.get(name="wants email")).
User_Table:
User
username
etc
Preferences_Table:
name
description
etc
UserPreferences_Table:
User (FK_User)
Preference (FK_Preferences)
Setting (Boolean)
Depending on your setup, you may be able to omit the Setting field in the UserPreferences table and simply use the existence of an entry for that User/Preference as a True and the lack of one as a False.

You could also use a bitmap. You only need single char field in you database. Somewhere in your app you store a list of preferences, pref1, pref2, pref3 ... and in the bitmap filed you store a sequence of 1's and 0's that correspond to the preferences.
For example 101 means pref1=yes, pref2=no, and pref3=yes and 011 means pref1=no, pref2=yes, and pref3=yes.
You could make this reusable by creating a new model field type for bitmaps.

" In the future when I want to add a new item, I don't want to have to add a new column in my database."
In this case, you'll want to add a row.
You have a table with the domain of possible setting Names. 50 rows.
You have a table of actual settings. User, Setting Name, Setting Value.

Related

AWS Personalize items attributes

I'm trying to implement personalization and having problems with Items schema.
Imagine I'm Amazon, I've products their brands and their categories. In what kind of Items schema should I include this information?
Should I include brand name as string as categorical field? Should I rather include brand ID as string or numeric? or should I include both?
What about categories? I've the same questions.
Metadata Fields Metadata includes string or non-string fields that
aren't required or don't use a reserved keyword. Metadata schemas have
the following restrictions:
Users and Items schemas require at least one metadata field,
Users and Interactions datasets can contain up to five metadata
fields. An Items dataset can contain up to 50 metadata fields.
If you add your own metadata field of type string, it must include the
categorical attribute. Otherwise, Amazon Personalize won't use the
field when training a model.
https://docs.aws.amazon.com/personalize/latest/dg/how-it-works-dataset-schema.html
There are simply 2 ways to include your metadata in Items/Users datasets:
If it can be represented as a number value, then provide the actual value if it makes sense.
If it can be represented as string, then provide the string value and make sure, that categorical is set to true.
But let's take a look into "Why does they need me, to categorize my strings metadata?". The answer is pretty simple.
Let's start with an example.
If you would have Items as Amazon.com products and you would like to provide rates metadata field, then:
You could take all of the rates including the full review text sent by clients and simply put it as metadata field.
You can take just stars rating, calculate the average and put it as metadata field.
Probably the second one is making more sense in general. Having random, long reviews of product as metadata, pretty much changes nothing. Personalize doesn't understands if the review itself is good or bad, or if the author also recommends another product, so pretty much it doesn't really add anything to the recommendations.
However if you simply "cut" your dataset and calculate the average rating, like in the 2. point, then it makes a lot more sense. Maybe some of our customers like crappy products? Maybe they want to buy them, because they are famous YouTubers and they create videos about that? Based on their previous interactions and much more, Personalize will be able to perform just slightly better, because now it knows, that this product has rating of 5/5 or 3/5.
I wanted to show you, that for some cases, providing Items metadata as string makes no sense. That's why your string metadata must be categorical. It means, that it should be finite set of values, so it adds some knowledge for Personalize about given Item and why some of people might want to interact with it.
Going back to your question:
Should I include brand name as string as categorical field? Should I rather include brand ID as string or numeric? or should I include both?
I would simply go with brand ID as string. You could also go with brand name, but probably single brand can be renamed, when it's still the same brand, so picking up the ID would be more constant. Also two different brands could have the same names, because they are present on different markets, so picking up the ID solves that.
The "categorical": true switch in your schema just tells Personalize:
Hey, do you see that string field? It's categorised, finite set of values. If you train a model for me, please include this one during the training, it's important!
And as it's said in documentation, if you will provide string metadata field, which is not marked as categorical, then Personalize will "think" that:
Hmm.. this field is a string, it has pretty random values and it's not marked as categorical. It's probably just a leftover from Items export job. Let's ignore that.

How to add a new field on a DynamoDB table?

I am a total beginner on DynamoDB and hardly know how to make a working query. But I recently came up with something which is apparently doing what I want.
Here is my question, I now have a table like this:
It has a primary partition key and a primary sort key:
Primary partition key
primaryPartitionIdKey (String)
Primary sort key
primarySortIdKey (String)
But two fields are not enough to do what I need. I would like to add one more.
Another field:
otherFieldIdKey (String)
Is that possible, if YES: how should I do it?
I can' see anything on the AWS console for that.
DynamoDB tables are schemaless, which means that neither the attributes nor their data types need to be defined beforehand. Each item can have its own distinct attributes.
So, your new "field" or attribute will be automatically created upon the first record put/update operation.
See DynamoDB Core Components.
Follow these steps to write data to the Music table using the DynamoDB console.
Open the DynamoDB console at
https://console.aws.amazon.com/dynamodb/.
In the navigation pane on the left side of the console, choose
Tables.
In the table list, choose the Music table.
Select View Items.
In the Items view, choose Create item.
Choose Add new attribute, and then choose Number. Name the field Awards.
Repeat this process to create an AlbumTitle of type String.
Enter the following values for your item:For Artist, enter No One You Know as the value. For SongTitle, enter Call Me Today. For AlbumTitle, enter Somewhat Famous. For Awards, enter 1.
Choose Create item.
Do this one more time to create another item with the same Artist as the previous step, but different values for the other attributes

dotCMS - How to create keys with two fields in content, Rest API save/publish with multiple content

In dotCMS,
1) I Created a content type with name product with 3 fields (name, id, description). Name and id fields created by selecting unique option. Name and Id fields combination should give uniqueness. But in dotCMS there is individual field unique option. When I try to add new content with below data it is failing to insert .
a) name: prod1, id : 1, description: phone.
b) name: prod2, id: 1, description: tablet.
2) While calling REST api to save/publish a content, is there any way to pass multiple input instead of single PUT or POST. https://dotcms.com/docs/latest/rest-api-end-points#ContentResource
Is there any solution for above two problems?
The Unique option applies only to a single field, and ensures each content item has a Unique value for that field only. There's no built-in way to check for uniqueness on multiple fields.
However one relatively simple way to do what you want is to create a separate custom field that automatically fills with the value of the name and ID fields (just append the value of the two fields), and then just set the Unique flag on the custom field. For more information on custom fields, see https://auth.dotcms.com/docs/latest/custom-field. Note that the dotCMS starter site includes many custom fields you can use as examples as well.
I'm not sure what you mean. If you mean can you make a single REST call to create or update multiple individual content items, then the answer is no - you need to do a separate REST call for each item you want to create or update. (So you'd either need to code a loop to create each piece of content, or use another method such as importing content using a CSV file).
If that's not what you mean, can you please clarify your question?

Dynamics AX 2012: Translate RecId into a Value

I asked a question similar to this previously (How to use RecId as a foreign key in a form) but would like to explore it a bit further in a more complex scenario.
Replacement keys work great when you have indexes set up and allow duplicates set to no, but they don't seem to work at all with multiple-field indexes or when allow duplicates is set to yes.
Is there way, programmatically, to replace a foreign key in a grid with a translated value without using replacement keys? I tried writing a display method to override the field, but some odd behavior resulted--fields moving around in the grid, and the display method being unaware of which row to use, thus all values in the entire column were the same.
Table A: Bob:1, Sally:1, Sue:3
Table B: 1:Apples, 2:Apples, 3:Oranges
The "people" are tied to their favorite "foods" by the food RecId, refererenced in the People table. Assume there is additional data in other columns that make these records unique, so consolidating "1:Apples" and "2:Apples" is not possible.
It seems there should be a way to write a display method to overwrite a field value in a grid. Any suggestions? Sample code?
Thanks
Firstly, surrogate FK replacement does (or at least should) work with composite keys (e.g., {First Name, Last Name}).
Secondly, you state that there is "additonal data in other columns" that make these records unique...Then why aren't these columns being combined with the food's name to form an alternate key? The data model seems incorrect (or at least some metadata isn't being made consistent with the conditions you've stated)
Thirdly, any Field Group can be chosen as the ReplacementFieldGroup on a Reference Group control. That alone will allow you to do basically whatever you want. That said, I would strongly encourage you to use an alternate key as your replacement field group whenever possible due to the semantics of surrogate FK replacement.
Flow:
1) User types a value(s) into reference group.
2) User's tabs out.
3) User's typed value(s) are used to look up a record in the related table.
4a) If the user's typed in value(s) are uniquely mapped to a record that record is chosen, else,
4b) If the user's typed in values are not unique a lookup is presented to allow the user to pick which record they "meant". Note that the lookup must therefore present a collection of uniquely identifiable records so that the user knows which record to pick (if the records all look the same in the lookup then they'll have no idea what in the hell they should pick.)
5) Upon successful resolution of the typed values, the record is set back on the source form.
Given this flow, it is obvious that steps 3-5 will be broken if there is no unique index (key) on the table. (How is the user supposed to specify a unique reference to the record if the record has no means of being uniquely identified (assuming you don't want to display RecId to the user)???)
In the exceptional case that you decide that you still want to use a non-unique index as your replacement field group you must implement resolveReference and lookupReference to provide the user a unique resolution/lookup experience (to handle steps 3-5 in the above flow). Note: The common use case for this is wanting to effectively eliminate non-selective fields from being displayed in Reference Group and instead letting some outer context or mode implicitly set that value. E.g., if the alternate key was {Size, Color}, one could potentially make "Color" a global form context--perhaps by having the user pick a color at the top of the form--and only have the user enter Size into Reference Group...The Color could then be implicitly added back via the resolveReference and lookupReference overrides.

Oracle APEX - Setting up a Tabular Form with default values

I pretty new to APEX and I'm having a bit of trouble working with my first Tabular form. The table I've linked it to is fairly simple. The columns are as follows:
Primary key representing an internal
code for a college major
Foreign key representing the "real"
code for the college major
Description for the college major
The user that inserted/updated the
row in the table
The date the row was inserted/updated
At the moment, I'm facing two problems.
I want the user to be able to specify their own primary key for the row but not to be able to change any existing primary keys. When I specify the column type as "Text Field" users are able to edit existing rows' primary keys and it also seems to break the report when trying to add a new row as I get a checksum error.
I would like the user and date
columns to default to the currently
logged in user and the current date,
but specifying default values for
either of these columns also seems
to cause syntax/SQL errors. Does
anyone have any examples of how to
use the default value functionality
for a column? Fixed. I can just use SYSDATE as a value on it's own when specifying the PL/SQL type for default. Username can be obtained through functions in APEX_UTIL
Perhaps you could use 2 conditional fields. If the field value is null, display the edit box, if the field value is not null display the display-only field.