Different results for concrete and abstract model - pyomo

I am new to Pysp and Pyomo. When I was trying to run the concrete model from the farmer example, I run the command suggested in the ReadMe file (except for changing the model from abstract to concrete) and I got a different result (98000) for the concrete model, from the abstract model (-118600) of the farmer problem.
I could get the same result for the abstract model on my computer as suggested in the ReadMe file. Does anyone have an idea about what has caused this issue? Did I do something wrong? Thank you!
Best,
Yeming
Output

Related

django oscar order app abstract models customization

I want to override django-oscar Order class which Inheritance from AbstractOrder class (in order apps and abstract_models.py file) but when i try to use migrate & makemigrations commands it says that model there is a problem in register_model function and i have no idea how i can solve it
(i need to use basket_total_incl_tax property and currency field does anyone know where can i find these two in other classes?)
if anyone knows what the problem is and what should i do to solve it please tell me.
thank you.

How to get list of methods in Odoo Models?

I'm using odooV10.
I want to know every model's methods without searching it manually, but I couldn't find anywhere to achieve this. Is anyone have any idea?
well you can simply dir(model)
ie.
any_method_on_your_model(self):
print dir(self)
you will see all the properties included in the object.
Activate the Developer Mode in OdooV10.
Then go Setting(odoo Main Settings)--> Technical -->Models.
Here you can find all models, fields, views and constraints. I hope this will work for you.
if not explain what you want trying to do.?
In python you can get method of class
get methods of class
but the problem here odoo inherits mechanism prevent you from doing
this. when you call a method a very complicated operation to create
an object with that method from the inherited classes

Check which fields are changed in Django

I have a model with many fields. I need to run a code only when two specific fields are changed. I know I can modify my model's save() method but I don't know how to check which fields are changed in my ModelForm.
Sorry because i cant give you any code snippet since i am not very experienced in Django, but i would go for getting a signal when a change is done through a controler class listening on the models and hearing for changes. Hope i might helped a bit.
Good luck
Django-model-utils has something called a FieldTracker that does exactly this. Just instantiate it in your model and tell it to track the specific fields you're looking for. Then you can use the has_changed method to test if a given field has changed since the last save.
This package has some other great utilities as well, I really recommend looking through the docs.
You could try django-dirtyfields.

Django - Polymorphic Models or one big Model?

I'm currently working on a model in Django involving one model that can have a variety of different traits depending on what kind of object it is. So, let's say we have a model called Mammal, which can either be an Elephant or a Dolphin (with their own traits "tusk_length" and "flipper_length" respectively).
Basic OOP principles shout "polymorphism", and I'm inclined to agree. But, as I'm new to Django, I first want to know whether or not it is the best way to do so in Django. I've heard of plenty of examples of and some people giving their preferences toward singular giant models
I've already tried using GenericForeignKeys as described here: How can I restrict Django's GenericForeignKey to a list of models?. While this solution works beautifully, I don't like the inability to filter, and that the relationship is only one way. That is, while you can get a Dolphin from a Mammal object, you can't get the Mammal object from the Dolphin.
And so, here are my two choices:
Choice A:
from django.db import models
class Mammal(models.Model):
hair_length = models.IntegerField()
tusk_length = models.IntegerField()
flipper_length = models.IntegerField()
animal_type = models.CharField(max_length = 15, choices = ["Elephant", "Dolphin"]
Choice B:
from django.db import models
class Mammal(models.Model):
hair_length = models.IntegerField()
class Elephant(Mammal):
tusk_length = models.IntegerField()
class Dolphin(Mammal):
flipper_length = models.IntegerField()
Choice B, from what I understand, has the advantage of nicer code when querying and listing all Elephants or Dolphins. However, I've noticed it's not as straightforward to get all of the Elephants from a list of Mammals (is there a query for this?) without putting animal_type in the class, with default being dependent on the class.
This leads to another problem I see with polymorphism, which won't come up in this example above or my application, but is worth mentioning is that it would be difficult to edit a Dolphin object into an Elephant without deleting the Dolphin entirely.
Overall, is there any general preference, or any big reason I shouldn't use polymorphism?
My recommendation, in general with database design, is to avoid inheritance. It complicates both the access and updates.
In Django, try using an abstract class for your base model. That means a db table will not be created for it. Its fields/columns will be auto-created in its child models. The benefit is: code reuse in Django/Python code and a simple, flat design in the database. The penalty is: it's more work to manage/query a mixed collection of child models.
See an example here: Django Patterns: Model Inheritance
Alternatively, you could change the concept of "Mammal" to "MammalTraits." And include a MammalTraits object inside each specific mammal class. In code, that is composition (has-a). In the db, that will be expressed as a foreign key.
We ended up going with a large table with a lot of usually-empty columns. Our reasoning was that (in this case) our Mammal table was all we'd be querying over, and there was no (intuitive) way to filter out by certain types of Mammals besides manually checking whether they had a "dolphin" or "elephant" object, which then threw an error if they didn't. Even looking for the type of an object returned from a query that was definitely an Elephant still returned "Mammal". It would be hard to extend any Pythonic workarounds to writing pure SQL, which one of our data guys does regularly.

How do I implement a common interface for Django related object sets?

Here's the deal:
I got two db models, let's say ShoppingCart and Order. Following the DRY principle I'd like to extract some common props/methods into a shared interface ItemContainer.
Everything went fine till I came across the _flush() method which mainly performs a delete on a related object set.
class Order(models.Model, interface.ItemContainer):
# ...
def _flush(self):
# ...
self.orderitem_set.all().delete()
So the question is: how do I dynamically know wheter it is orderitem_set or shoppingcartitem_set?
First, here are two Django snippets that should be exactly what you're looking for:
Model inheritance with content type and inheritance-aware manager
ParentModel and ChildManager for Model Inheritance
Second, you might want to re-think your design and switch to the django.contrib content types framework which has a simple .model_class() method. (The first snippet posted above also uses the content type framework).
Third, you probably don't want to use multiple inheritance in your model class. It shouldn't be needed and I wouldn't be surprised if there were some obscure side affects. Just have interface.ItemContainer inherit from models.Model and then Order inherit from only interface.ItemContainer.
You can set the related_name argument of a ForeignKey, so if you want to make minimal changes to your design, you could just have ShoppingCartItem and OrderItem set the same related_name on their ForeignKeys to ShoppingCart and Order, respectively (something like "item_set"):
order = models.ForeignKey(Order, related_name='item_set')
and
cart = models.ForeignKey(ShoppingCart, related_name='item_set')