Saturday, 1 January 2022

How to create a django queryset object for inner join?

I have two tables.

class DibbsSpiderDibbsMatchedProductFieldsDuplicate(models.Model):
    nsn = models.TextField()
    nsn2 = models.TextField()
    cage = models.TextField()
    part_number = models.TextField()
    company_name = models.TextField(blank=True, null=True)
    supplier = models.TextField(db_column='Supplier', blank=True, null=True)  # Field name made lowercase.
    cost = models.CharField(db_column='Cost', max_length=15, blank=True, null=True)  # Field name made lowercase.
    list_price = models.CharField(db_column='List_Price', max_length=15, blank=True, null=True)  # Field name made lowercase.
    gsa_price = models.CharField(db_column='GSA_Price', max_length=15, blank=True, null=True)  # Field name made lowercase.
    hash = models.TextField()
    nomenclature = models.TextField()
    technical_documents = models.TextField()
    solicitation = models.CharField(max_length=32)
    status = models.CharField(max_length=16)
    purchase_request = models.TextField()
    issued = models.DateField()
    return_by = models.DateField()
    file = models.TextField()
    vendor_part_number = models.TextField()
    manufacturer_name = models.TextField(blank=True, null=True)
    product_name = models.TextField(blank=True, null=True)
    unit = models.CharField(max_length=15, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'dibbs_spider_dibbs_matched_product_fields_duplicate'


class DibbsSpiderSolicitation(models.Model):
    line_items = models.IntegerField()
    nsn = models.TextField()
    nomenclature = models.TextField()
    technical_documents = models.TextField()
    purchase_request = models.TextField()

    class Meta:
        managed = False
        db_table = 'dibbs_spider_solicitation'

What will be the equivalent django query for the inner join of two tables on the column nsn? My views function will be like

def inner(request,nsn):
    u_m =  DibbsSpiderDibbsMatchedProductFieldsDuplicate.objects.filter(nsn2__icontains=id)
    c_m = DibbsSpiderSolicitation.objects.filter(nsn__icontains=id)
    obj = .......................
    context = {'obj':obj}
    return render(request,,"a.html",context)

the queryset should return the combination of two tables according to the common nsn.

the obj should return the combination of u_m and c_m. If u_m contains only one rows and c_m contains many rows then the obj must replicate the values of u_m.



from How to create a django queryset object for inner join?

The app is freezing for a little bit when using Retrofit with RxJava

JSON

[
   {
      "countryName":"..."
   },
   {
      "countryName":"..."
   },
   {
      "countryName":"..."
   } //etc... to 195 countries
]

Interface

public interface RetrofitInterface {

    @GET("GetCountries.php")
    Single<List<CountryModel>> getCountries();

}

Code

new Retrofit.Builder().baseUrl(Constants.BASE_URL).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJava3CallAdapterFactory.create()).build().create(RetrofitInterface.class).getCountries().doOnSuccess(countryModels - > {
    for (CountryModel item: countryModels) {
        Chip chip = new Chip(requireContext());
        chip.setText(item.getCountryName());
        fragmentCountriesBinding.fragmentCountriesChipGroupMain.addView(chip);
    }
}).observeOn(AndroidSchedulers.mainThread()).subscribe(new SingleObserver < List < CountryModel >> () {
    @Override
    public void onSubscribe(@io.reactivex.rxjava3.annotations.NonNull Disposable d) {
        
    }

    @Override
    public void onSuccess(@io.reactivex.rxjava3.annotations.NonNull List < CountryModel > countryModels) {
        
    }

    @Override
    public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {
        
    }
});

I'm trying to add 195 countries to the ChipGroup but the app is freezing for a little bit during adding the chips, Firstly the code inside the doOnSuccess method was in the onSuccess method but the app was freezing for a little bit, So the code has been moved to doOnSuccess method but I get this message Only the original thread that created a view hierarchy can touch its views.

I'm new with RxJava, Any solutions?



from The app is freezing for a little bit when using Retrofit with RxJava

Firestore Transaction contention without write attempts?

I am analyzing logs that show the error "Too much contention on these documents. Please try again". What is strange here is that logs within the transactions never executed, which seems to indicate the transactions never made a write attempt. In the below example, the error says there was contention at runTransaction though "Updating documents" is not in any of the logs.

If there was no write operation performed on the documents (note these transactions are the only way these documents are updated), how is it possible there was contention? That is, if there were never any writes, given transactions allow reads from outside of the transaction how could the below code lead to contention? Obviously I understand that a few of these transactions on the same documents trying to write would trigger contention, but in this case, "Updating documents" never executed so it isn't possible there was ever a write attempt.

return db.runTransaction((transaction) => {
   return transaction.get(query).then((querySnapshot) => {
      console.log("Updating documents");
      [transaction code here to update documents]
   }
});


from Firestore Transaction contention without write attempts?