Error when requesting User.history.all () - django

I have a model class User (AbstractUser), I would like to keep track of the User history, but I have an error while getting User.history.all (),
ERROR: table "core_user" is missing from the FROM clause
LINE 1: SELECT "core_user". "Id", "core_user". "Password", "core_user" ...
SELECT "core_user"."id", "core_user"."password", "core_user"."last_login", '
'"core_user"."is_superuser", "core_user"."username", '
'"core_user"."first_name", "core_user"."last_name", "core_user"."email", '
'"core_user"."is_staff", "core_user"."is_active", "core_user"."date_joined", '
'"core_user"."editor_config", "dib_page_historicaluser"."ip_address", '
'"dib_page_historicaluser"."history_id", '
'"dib_page_historicaluser"."history_date", '
'"dib_page_historicaluser"."history_change_reason", '
'"dib_page_historicaluser"."history_type", '
'"dib_page_historicaluser"."history_user_id" FROM "dib_page_historicaluser" '
'ORDER BY "dib_page_historicaluser"."history_date" DESC, '
'"dib_page_historicaluser"."history_id" DESC LIMIT 21')
core_user table is my base_model, and dib_page_historicaluser table is the history model.

Related

Microsoft Access: Group By Function and list out variances [duplicate]

I have a table that contains run numbers. This is then linked to a second table that contains the serial numbers of the panels that go through each run. I was wondering is it possible to design a query that will give for each run number the serial numbers.
I would like it in a table like:
Run Number1, First Serial Number for 1, Second Serial Number for 1, etc..
Run Number2, First Serial Number for 2, Second Serial Number for 2, etc..
I can get in in the form:
Run Number1, First Serial Number for 1
Run Number1, Second Serial Number for 1
Run Number2, First Serial Number for 2
Run Number2, Second Serial Number for 2
Is there a way to set this up?
You can use my DJoin function as this will accept SQL as the source, thus you won't need additional saved queries:
' Returns the joined (concatenated) values from a field of records having the same key.
' The joined values are stored in a collection which speeds up browsing a query or form
' as all joined values will be retrieved once only from the table or query.
' Null values and zero-length strings are ignored.
'
' If no values are found, Null is returned.
'
' The default separator of the joined values is a space.
' Optionally, any other separator can be specified.
'
' Syntax is held close to that of the native domain functions, DLookup, DCount, etc.
'
' Typical usage in a select query using a table (or query) as source:
'
' Select
' KeyField,
' DJoin("[ValueField]", "[Table]", "[KeyField] = " & [KeyField] & "") As Values
' From
' Table
' Group By
' KeyField
'
' The source can also be an SQL Select string:
'
' Select
' KeyField,
' DJoin("[ValueField]", "Select ValueField From SomeTable Order By SomeField", "[KeyField] = " & [KeyField] & "") As Values
' From
' Table
' Group By
' KeyField
'
' To clear the collection (cache), call DJoin with no arguments:
'
' DJoin
'
' Requires:
' CollectValues
'
' 2019-06-24, Cactus Data ApS, Gustav Brock
'
Public Function DJoin( _
Optional ByVal Expression As String, _
Optional ByVal Domain As String, _
Optional ByVal Criteria As String, _
Optional ByVal Delimiter As String = " ") _
As Variant
' Expected error codes to accept.
Const CannotAddKey As Long = 457
Const CannotReadKey As Long = 5
' SQL.
Const SqlMask As String = "Select {0} From {1} {2}"
Const SqlLead As String = "Select "
Const SubMask As String = "({0}) As T"
Const FilterMask As String = "Where {0}"
Static Values As New Collection
Dim Records As DAO.Recordset
Dim Sql As String
Dim SqlSub As String
Dim Filter As String
Dim Result As Variant
On Error GoTo Err_DJoin
If Expression = "" Then
' Erase the collection of keys.
Set Values = Nothing
Result = Null
Else
' Get the values.
' This will fail if the current criteria hasn't been added
' leaving Result empty.
Result = Values.Item(Criteria)
'
If IsEmpty(Result) Then
' The current criteria hasn't been added to the collection.
' Build SQL to lookup values.
If InStr(1, LTrim(Domain), SqlLead, vbTextCompare) = 1 Then
' Domain is an SQL expression.
SqlSub = Replace(SubMask, "{0}", Domain)
Else
' Domain is a table or query name.
SqlSub = Domain
End If
If Trim(Criteria) <> "" Then
' Build Where clause.
Filter = Replace(FilterMask, "{0}", Criteria)
End If
' Build final SQL.
Sql = Replace(Replace(Replace(SqlMask, "{0}", Expression), "{1}", SqlSub), "{2}", Filter)
' Look up the values to join.
Set Records = CurrentDb.OpenRecordset(Sql, dbOpenSnapshot)
CollectValues Records, Delimiter, Result
' Add the key and its joined values to the collection.
Values.Add Result, Criteria
End If
End If
' Return the joined values (or Null if none was found).
DJoin = Result
Exit_DJoin:
Exit Function
Err_DJoin:
Select Case Err
Case CannotAddKey
' Key is present, thus cannot be added again.
Resume Next
Case CannotReadKey
' Key is not present, thus cannot be read.
Resume Next
Case Else
' Some other error. Ignore.
Resume Exit_DJoin
End Select
End Function
' To be called from DJoin.
'
' Joins the content of the first field of a recordset to one string
' with a space as delimiter or an optional delimiter, returned by
' reference in parameter Result.
'
' 2019-06-11, Cactus Data ApS, Gustav Brock
'
Private Sub CollectValues( _
ByRef Records As DAO.Recordset, _
ByVal Delimiter As String, _
ByRef Result As Variant)
Dim SubRecords As DAO.Recordset
Dim Value As Variant
If Records.RecordCount > 0 Then
While Not Records.EOF
Value = Records.Fields(0).Value
If Records.Fields(0).IsComplex Then
' Multi-value field (or attachment field).
Set SubRecords = Records.Fields(0).Value
CollectValues SubRecords, Delimiter, Result
ElseIf Nz(Value) = "" Then
' Ignore Null values and zero-length strings.
ElseIf IsEmpty(Result) Then
' First value found.
Result = Value
Else
' Join subsequent values.
Result = Result & Delimiter & Value
End If
Records.MoveNext
Wend
Else
' No records found with the current criteria.
Result = Null
End If
Records.Close
End Sub
Full documentation can be found in my article:
Join (concat) values from one field from a table or query
If you don't have an account, browse to the link: Read the full article.
Code is also on GitHub: VBA.DJoin

How to avoid this empty row using QUERY() in Google Sheets?

I've tried applying some criteria, but the row is still there.
This first empty row shouldn't be there.
Here is the formula, in case you'd like to help me correct it:
=if(I4<>"", { IFERROR(query(Datasets!X3:AU,"select AS, AT, AJ, AR where AS matches date '"&TEXT(DATEVALUE(I4),"yyyy-mm-dd")&"'",0),{"","","",""}); IFERROR(query(Datasets!BC3:BT,"select BC, BD, BS, BQ where BC matches date '"&TEXT(DATEVALUE(I4),"yyyy-mm-dd")&"'",0),{"","","",""}) }, UNIQUE({query(Datasets!X3:AU,"select AS, AT, ' ', SUM(AR) where AS is not null group by AS, AT, ' ' label ' '' ', SUM(AR) ' '",0); query(Datasets!BC3:BT,"select BC, BD, ' ', SUM(BQ) where BC is not null group by BC, BD, ' ' label ' '' ', SUM(BQ) ' '",0) }))
try:
=IF(I4<>"", {IFERROR(QUERY(Datasets!X3:AU,
"select AS,AT,AJ,AR
where AS matches date '"&TEXT(DATEVALUE(I4), "yyyy-mm-dd")&"'", ), {"","","",""});
IFERROR(QUERY(Datasets!BC3:BT,
"select BC,BD,BS,BQ
where BC matches date '"&TEXT(DATEVALUE(I4), "yyyy-mm-dd")&"'", ), {"","","",""})},
UNIQUE({QUERY(Datasets!X3:AU,
"select AS,AT,' ',SUM(AR)
where AS is not null
group by AS,AT,' '
label' ''',SUM(AR)''", );
QUERY(Datasets!BC3:BT,
"select BC,BD,' ',SUM(BQ)
where BC is not null
group by BC,BD,' '
label' ''',SUM(BQ)''", )}))

Oracle SQL - Replace space/alpanumeric/non-alphanumeric with space and replace with word?

Hi I want to replace following examples with the following:
(LTRIM) Replace any space at the beginning before first Alphanumerical character
(RTRIM) Replace any space at the end after last Alphanumerical character
Replace any non-alpha numerical with space
Replace any empty (not null) with word "UNKNOWN"
In case of only space replace all spaces with word "UNKNOWN"
' abc ' -> 'abc'
'abc ' -> 'abc'
' abc ' -> 'abc'
'!ab c ? ' -> 'ab c'
' a b c ' -> 'a b c'
'a!b?c $ ' -> 'a b c'
' ' -> 'UNKNOWN'
'' -> 'UNKNOWN'
null -> null
These are the queries I'm working on but I'm not getting anywhere:
select
'-|&#/,.‘“<>():;' as default1,
translate(SUBSTR(MAX ('-|&#/,.‘“<>():;'),1,70), '-|&#/,.‘“<>():;', ' ') as formatted1
from dual;
select trim(regexp_replace(regexp_replace(' a b cdefgh ' , '[[:space:]]*',''), '(.)', '\1 UNK' )) as formatted from dual;
SELECT LTRIM(RTRIM(' NEXT LEVEL EMPLOYEE ')) from dual;
Replace any empty (not null) with word "UNKNOWN"
This is impossible as, in Oracle, there are no empty strings as an empty string is stored as NULL.
Apart from that impossibility, you can use:
SELECT value,
CASE
WHEN value IS NULL
THEN NULL
ELSE COALESCE(
TRIM(
BOTH ' ' FROM
REGEXP_REPLACE(
value,
'[^A-Za-z0-9]',
' '
)
),
'UNKNOWN'
)
END AS updated_value
FROM table_name;
Which, for the sample data:
CREATE TABLE table_name (value) AS
SELECT ' abc ' FROM DUAL UNION ALL
SELECT 'abc ' FROM DUAL UNION ALL
SELECT ' abc ' FROM DUAL UNION ALL
SELECT '!ab c ? ' FROM DUAL UNION ALL
SELECT ' a b c ' FROM DUAL UNION ALL
SELECT 'a!b?c $ ' FROM DUAL UNION ALL
SELECT ' ' FROM DUAL UNION ALL
SELECT '' FROM DUAL UNION ALL
SELECT null FROM DUAL;
Outputs:
VALUE
UPDATED_VALUE
    abc
abc
abc
abc
  abc
abc
!ab c ?
ab c
 a b c
a b c
a!b?c $
a b c
         
UNKNOWN
null
null
null
null
fiddle

Problems with strip function in generating list of lists

I'm having a slight problem with this function. I'm trying to generate a list of lists. For each item in those lists I need to remove the space to the left and to the right of each item. For some reason it's only working for the first item in each list.
def filereadlistoflists():
master_list=[]
input_file = open('movies.txt','r')
for line in input_file:
add_line = line.strip().split(",")
master_list.append(add_line)
return master_list
Here's the result:
[['Brad Pitt', ' Sleepers', ' Troy', ' Meet Joe Black', ' Oceans Eleven', ' Seven', ' Mr & Mrs Smith'], ['Tom Hanks', ' You have got mail', ' Apollo 13', ' Sleepless in Seattle', ' Catch Me If You Can']

Python- How to change lists order in a list of lists

I have a list of lists that i want to put in order with the times that a person leaves he's job. I already made a code to give me only the time but i need something to check the times and put them in order.
inFile=removeHeader(file_name) # the information is taken from a txt file and this only gives me the part of the services
#print inFile gives me list of lists like [['Peter', ' 06-CB-89', ' Xavier', ' 09:45', ' 10:15', ' downtown', ' 10', ' standby'], ['Robert', ' 13-KI-54', ' Paul', ' 09:30', ' 10:30', ' Castle', ' 45', ' standby']
for time in inFile:
hours=time[4]
print hours #this gives me only the hours they left work
see https://docs.python.org/2/library/functions.html#sorted
you have to sort the main list by the leave time i.e. the 5th value of the sublists, the key argument of sorted alows you to specify a function that will be called to compute keys the list is sorted based on:
import time
x =[['Peter', ' 06-CB-89', ' Xavier', ' 09:45', ' 10:15', ' downtown', ' 10', ' standby'], ['Robert', ' 13-KI-54', ' Paul', ' 09:30', ' 10:30', ' Castle', ' 45', ' standby']]
sorted(x, key = lambda x: x[4])