Django Admin Change Form : How To avoid using __str__ in header of Django Admin Change Form? - django-admin

Hi does anyone know if it is possible to avoid Django rendering str for model when in the change admin form? My str method has multiple attributes rendered as shown below:
class Product(models.Model):
"""
Product
"""
id = models.PositiveBigIntegerField(primary_key=True)
attributes = models.JSONField()
labels = models.JSONField()
name = models.TextField()
relative_url = models.TextField(max_length=250)
image = models.URLField()
delivery = models.TextField(max_length=50)
online = models.BooleanField()
is_customizable = models.BooleanField()
is_exclusive = models.BooleanField()
url = models.URLField()
max_price = models.PositiveIntegerField()
min_price = models.PositiveIntegerField()
currency = models.ForeignKey(Currency, on_delete=models.CASCADE)
discount_percentage = models.PositiveSmallIntegerField(default=0)
recommended_retail_price = models.PositiveIntegerField()
def __str__(self) -> str:
product_str = (
f"id: {self.id}\n"
f"attributes: {self.attributes}\n"
f"labels: {self.labels}\n"
f"name: {self.name}\n"
f"relative_url: {self.relative_url}\n"
f"image: {self.image}\n"
f"delivery: {self.delivery}\n"
f"online: {self.online}\n"
f"is_customizable: {self.is_customizable}\n"
f"is_exclusive: {self.is_exclusive}\n"
f"url: {self.url}\n"
f"max_price: {self.max_price}\n"
f"min_price: {self.min_price}\n"
f"currency: {self.currency}\n"
f"discount_percentage: {self.discount_percentage}\n"
f"recommended_retail_price: {self.recommended_retail_price}\n"
)
return product_str

Related

How to avoid using too much parameter in mutate() function?

Mutate function takes a lot of arguments while performing an update operation. According to the official graphene documentation, I could use (parent, root, obj) object to access my class objects.
I tried parent, root and obj to accessing variable data but it didn't work.
class UpdateEvent(graphene.Mutation):
ok = graphene.Boolean()
class Arguments:
id = graphene.ID()
name = graphene.String()
event_start_date = graphene.types.datetime.Date()
event_finish_date = graphene.types.datetime.Date()
event_mark_as_finished = graphene.Boolean()
event_description = graphene.String()
event_location = graphene.String()
event_price = graphene.Float()
event_price_unit = graphene.String()
def mutate(root, info):
event = Events.objects.get(pk=root.id)
event.name = root.name
event.event_start_date = root.event_start_date
event.event_finish_date= root.event_finish_date
event.event_mark_as_finished = root.event_mark_as_finished
event.event_description = root.event_description
event.event_location = root.event_location
event.event_price = root.event_price
event.event_price_unit = root.event_price_unit
event.save()
# This class registered to the Mutation class***
"errors": [{ "message": "mutate() got an unexpected keyword argument 'event_description'"}]
Putting all variables into the mutate function works perfectly but this is an ugly way.
How can I handle this?
Wrap them with a single input type:
class UpdateEventMutationInput(graphene.InputObjectType):
id = graphene.ID()
name = graphene.String()
event_start_date = graphene.types.datetime.Date()
event_finish_date = graphene.types.datetime.Date()
event_mark_as_finished = graphene.Boolean()
event_description = graphene.String()
event_location = graphene.String()
event_price = graphene.Float()
event_price_unit = graphene.String()
And use it in mutation:
class UpdateEvent(graphene.Mutation):
ok = graphene.Boolean()
class Arguments:
data = UpdateEventMutationInput(required=True)
def mutate(root, info, data):
event = Events.objects.get(pk=data.id)
event.name = data.name
event.event_start_date = data.event_start_date
event.event_finish_date= data.event_finish_date
event.event_mark_as_finished = data.event_mark_as_finished
event.event_description = data.event_description
event.event_location = data.event_location
event.event_price = data.event_price
event.event_price_unit = data.event_price_unit
event.save()
It also brings such advantages:
less code in mutation
much easy to write a request for this mutation (it would contain a single $data: UpdateEventMutationInput! variable)
less cross-version conflicts because you don't have to update the request as mutation input changes

TYPO3 Show fe_user with Hyperlink and GET parameter

I'm using TYPO3 7.6 and I'd like to send an email with a Hyperlink to my TYPO3-Website (page=123). On this page I've listed all fe_users with the following TypoScript, but I'll only show one specific user, f.e. uid=20.
I have two questions:
How can I show only this user (uid) on my page?
How do I call my hyperlink?
Show all users:
lib.feUserLink = CONTENT
lib.feUserLink {
table = fe_users
select.pidInList = 10
select.max = 10
select.orderBy = last_name DESC
where = disable = 0
renderObj = COA
renderObj {
10 = TEXT
10.field = first_name
10.wrap = wq<p><strong>|
...
14 = TEXT
14.field = email
14.wrap = |</p>
}
}
-> 1. Then I have to edit my WHEREclause, but i dont't know how? All of my tests won't work.
lib.feUserLink = CONTENT
lib.feUserLink {
table = fe_users
...
andWhere.dataWrap = uid={GP:fe_users|uid}
#andWhere.data = GP:feuseruid
#andWhere.data = GP:fe_user|uid
#andWhere.intval = 1
#andWhere.wrap = uid=|
...
-> 2. And how can I show only the User from the GET-Parameter?
http://my.domain.tld/index.php?id=123&uid=20
http://my.domain.tld/index.php?id=123&feuser=20
http://my.domain.tld/index.php?id=123&fe_users[uid]=20
...
I hope someone can give me a crucial tip ...
... thank you.
Regards
Stefan
my solution was
lib.feuserLink = CONTENT
lib.feUserLink {
table = fe_users
select {
pidInList = 10
orderBy = last_name DESC
where = uid = ###field_uid###
markers.field_uid.data = GP:user
markers.field_uid.intval = 1
max = 1
}
...
Thanks for helping ...
2) Build link to page 123 with uid parameter from fe_user. Adds something like
?id=123&user=456&cHash=789
lib.feUserLink = CONTENT
lib.feUserLink {
table = fe_users
select {
pidInList = 10
max = 10
orderBy = last_name DESC
}
renderObj = COA
renderObj {
10 = TEXT
10.field = username
10.typolink {
parameter = 123
additionalParams.dataWrap = &user={field:uid}
useCacheHash = 1
}
10.wrap = |</br>
}
}
1) To show only selected user in given list, adapt your select like
[globalVar = GP:user > 0]
lib.feUserLink.select {
where = uid = ###field_uid###
markers.field_uid.data = GP:user
markers.field_uid.intval = 1
max = 1
}
[global]
You don't need clause where = disable = 0. Its already included in the SQL query by default.

Wrong Number of Arguments

I understand what causes the wrong number of arguments error but my code doesn't pass any parameters to initialize any of the classes so I'm not sure at all why my code is giving me this error. I'm also pretty new to Ruby on Rails so that doesn't help things. My code is below:
def create_google_file
#products = Product.find(:all)
file = File.new('dir.xml','w')
doc = REXML::Document.new
root = REXML::Element.new "rss"
root.add_attribute("xmlns:g", "http://base.google.com/ns/1.0")
root.add_attribute("version", "2.0")
channel = REXML::Element.new "channel"
root.add_element channel
title = REXML::Element.new "title"
title.text = "Sample Google Base"
channel.add_element title
link = REXML::Element.new "link"
link.text = "http://base.google.com/base/"
channel.add_element link
description = REXML::Element.new "description"
description.text = "Information about products"
channel.add_element description
#products.each do |y|
item = channel.add_element("item")
id = item.add_element("g:id")
id.text = y.id
title = item.add_element("title")
title.text = y.title
description = item.add_element("description")
description.text = y.description
googlecategory = item.add_element("g:google_product_category")
googlecategory.text = y.googlecategory
producttype = item.add_element("g:product_type")
producttype.text = y.producttype
link = item.add_element("link")
link.text = y.link
imglink = item.add_element("g:image_link")
imglink.text = y.imglink
condition = item.add_element("condition")
condition.text = y.condition
availability = item.add_element("g:availability")
availability.text = y.availability
price = item.add_element("g:price")
price.text = y.price "USD"
gtin = item.add_element("g:gtin")
gtin.text = y.gtin
brand = item.add_element("g:brand")
brand.text = y.brand
mpn = item.add_element("g:mpn")
mpn.text = y.mpn
expirationdate = item.add_element("g:expiration_date")
expirationdate.text = y.salepricedate
end
doc.add_element root
file.puts doc
file.close
end
The error I'm getting is:
ArgumentError in ProductsController#create_google_file
wrong number of arguments (1 for 0)
At the request of the poster, I am putting my comments in to an answer:
Based purely on the consistency of the other lines, but without knowing which line is actually failing, it may be this part: price.text = y.price "USD". Is y.price a method that takes in a parameter? Is it defined as def price(type) or something? If not, if it doesn't take any parameters, then it's because you're not supposed to send any parameters to that method. It looks like it's just a getter.
#FranklinJosephMoormann As I suspected, that's the line. Were you trying to make a string like "4.50 USD"? Then you probably wanted: price.text = "#{y.price} USD". That will take the result of y.price and put it in a string, and allow you to keep typing more in the string. It's called string interpolation.

dao recordset updating the wrong record

I'm trying to have a form usable for both creating a new record or updating another. Currently it is doing it through the value of a textbox (new or edit). The structure works fine, but for some reason, when it is performing the edit function, it is saving changes to the wrong record. For instance, if I am editing record 1027, when i submit it, it'll update record 1073. Its consistent, it'll always update the same, wrong record. Edit 1000, it'll update 1073; if i update 1081, it'll update 1073, and so on. Is there a way to specify which record it should be editing? yes, the record number is the primary key/id. Heres the code:
Private Sub btnSubmit_Click()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strTable As String
Dim strField As String
Dim ID As Long
Dim newID As Long
strTable = "record_holdData"
Set db = CurrentDb
Set rs = db.OpenRecordset(strTable)
'button has 2 modes
If txtMode.Value = "NEW" Then
With rs
.AddNew
.Fields("PO_no") = txtPONum
.Fields("prodSupervisor") = cboProdSup
.Fields("qaSupervisor") = cboQASup
.Fields("labTech") = cboLabTech
.Fields("flavor") = cboFlavor
.Fields("lineNumber") = cboLineNumber
.Fields("container") = cboContainer
.Fields("package") = cboPackage
.Fields("holdQty") = txtQty
.Fields("productionDate") = txtProdDate
.Fields("dateCode") = txtDatecode
.Fields("component") = cboComponent
.Fields("nonconformance") = cboDiscrepancy
.Fields("foundDuring") = cboFoundAt
.Fields("responsibility") = cboRespCode
.Fields("comments") = txtDescription
.Fields("rootCause") = txtRootCause
.Fields("holdStatus") = 1
.Fields("dateOpened") = Now()
.Update
.Bookmark = .LastModified
newID = !ID
End With
MsgBox ("Hold information saved!")
btnPrintTag.Enabled = True
DoCmd.OpenReport "Holdtag", acViewPreview, , "[ID] = " & newID
DoCmd.Close
ElseIf txtMode.Value = "EDIT" Then
'do editing stuff
With rs
.Edit
.Fields("PO_no") = txtPONum
.Fields("prodSupervisor") = cboProdSup
.Fields("qaSupervisor") = cboQASup
.Fields("labTech") = cboLabTech
.Fields("flavor") = cboFlavor
.Fields("lineNumber") = cboLineNumber
.Fields("container") = cboContainer
.Fields("package") = cboPackage
.Fields("holdQty") = txtQty
.Fields("productionDate") = txtProdDate
.Fields("dateCode") = txtDatecode
.Fields("component") = cboComponent
.Fields("nonconformance") = cboDiscrepancy
.Fields("foundDuring") = cboFoundAt
.Fields("responsibility") = cboRespCode
.Fields("comments") = txtDescription
.Fields("rootCause") = txtRootCause
.Fields("lastEditDate") = Now()
.Update
End With
MsgBox ("Information Updated")
End If
End Sub
Sorry i caught it. Problem was I was basically redefining the recordset each time the subroutine was called. I changed the second block to the following:
ElseIf txtMode.Value = "EDIT" Then
'do editing stuff
Set rs = db.OpenRecordset("SELECT * FROM record_holdData WHERE ID=" & txtID)
With rs
.Edit
.Fields("PO_no") = txtPONum
.Fields("prodSupervisor") = cboProdSup
.Fields("qaSupervisor") = cboQASup
.Fields("labTech") = cboLabTech
.Fields("flavor") = cboFlavor
.Fields("lineNumber") = cboLineNumber
.Fields("container") = cboContainer
.Fields("package") = cboPackage
.Fields("holdQty") = txtQty
.Fields("productionDate") = txtProdDate
.Fields("dateCode") = txtDatecode
.Fields("component") = cboComponent
.Fields("nonconformance") = cboDiscrepancy
.Fields("foundDuring") = cboFoundAt
.Fields("responsibility") = cboRespCode
.Fields("comments") = txtDescription
.Fields("rootCause") = txtRootCause
.Fields("lastEditDate") = Now()
.Update
End With

Need help in transforming this SQL statement to GORM

select
b.security_type,
b.symbol,
b.security_description,
b.trade_date_qty as 'axys_qty',
c.trade_date_qty as 'fidelity_qty',
c.trade_date_qty - b.trade_date_qty as 'qty_diff',
b.cost_basis as 'axys_cost',
c.cost_basis as 'fidelity_cost',
c.cost_basis - b.cost_basis as 'cost_diff'
from
account a
inner join advent_position b on a.fixed_account_number = b.account_number
inner join fidelity_position c on a.fixed_account_number = c.account_number and b.symbol = c.symbol
where
b.account_number = '636296651'
Basically, I have the ff. domains: Account, AdventPosition, FidelityPosition. I haven't set the relationship yet. I'm just wondering if there's a way to replicate the logic above using Criteria or HQL. Forgive me, I'm still new to Grails.
Thank you for any leads on this.
It'd be something close to this:
String hql = '''
select
b.securityType,
b.symbol,
b.securityDescription,
b.tradeDateQty,
c.tradeDateQty,
c.tradeDateQty - b.tradeDateQty,
b.costBasis,
c.costBasis,
c.costBasis - b.costBasis
from
Account a, AdventPosition b, FidelityPosition c
where
a.fixedAccountNumber = b.accountNumber
and a.fixedAccountNumber = c.accountNumber
and b.symbol = c.symbol
and b.accountNumber = :accountNumber
'''
def accountNumber = '636296651'
def results = Account.executeQuery(hql, [accountNumber: accountNumber])
The results will be an ArrayList of Object[], so you can iterate it with something like
for (row in results) {
def securityType = row[0]
def symbol = row[1]
def securityDescription = row[2]
def axys_qty = row[3]
def fidelity_qty = row[4]
def qty_diff = row[5]
def axys_cost = row[6]
def fidelity_cost = row[7]
def cost_diff = row[8]
}
I replaced the hard-coded account number with a named parameter; you can use regular ? like in SQL if you prefer and run 'def results = Account.executeQuery(hql, [accountNumber])', and of course if you intented it to be hard-coded then restore that and don't pass in the 2nd parameter, just run 'def results = Account.executeQuery(hql)'
just sharing the solution that I came up (while waiting for an answer :P) but note that the previous answer is way much better and faster:
def acc = Account.findByFixedAccountNumber('636296651')
List advPos = AdventPosition.findAllByAccountNumber('636296651')
List fidPos = advPos.collect {
FidelityPosition.findAllByAccountNumberAndSymbol('636296651', it.symbol)
}
def item = [:]
def res = []
def limit = advPos.size() - 1
for(i in 0..limit){
item.security_type = advPos[i].securityType
item.symbol = advPos[i].symbol
item.security_description = advPos[i].securityDescription
item.axys_qty = advPos[i].tradeDateQty
item.fidelity_qty = fidPos[i].tradeDateQty
item.qty_diff = item.fidelity_qty - item.axys_qty
item.axys_cost = advPos[i].costBasis
item.fidelity_cost = fidPos[i].costBasis
item.cost_diff = item.fidelity_cost - item.axys_cost
res.add(item)
}

Resources